I want to display the actual image of an attachment, not the url of the attachment. I am using this code but it is displaying the url:
<?php
$argsThumb = array(
'order' => 'ASC',
'post_type' => 'attachment',
'post_parent' => $post->ID,
'post_status' => null
);
$attachments = get_posts($argsThumb);
if ($attachments) {
foreach ($attachments as $attachment) {
apply_filters('the_title', $attachment->post_title);
echo wp_get_attachment_url($attachment->ID, false, false);
}
}
?>
Perhaps try the following:
<?php
$argsThumb = array(
'order' => 'ASC',
'post_type' => 'attachment',
'post_parent' => $post->ID,
'post_status' => null
);
$attachments = get_posts($argsThumb);
if ($attachments) {
foreach ($attachments as $attachment) {
apply_filters('the_title', $attachment->post_title);
echo "<img src='" . wp_get_attachment_url($attachment->ID, false, false) . "' />";
}
}
?>
Related
How can I add an else statement to this code so if there are no posts, the output would be "There are currently no posts."
function jobs_posts_shortcode($atts, $content = null) {
global $post;
extract(shortcode_atts(array(
'category_name' => 'jobs',
'num' => '5',
'order' => 'DESC',
'orderby' => 'post_date',
), $atts));
$args = array(
'category_name' => $category_name,
'posts_per_page' => $num,
'order' => $order,
'orderby' => $orderby,
);
$output = '';
$posts = get_posts($args);
foreach($posts as $post) {
setup_postdata($post);
$output .= ''.get_the_title().'<br>'.get_the_date().'<br><br>';
}
wp_reset_postdata();
return ''. $output .'';
}
add_shortcode('jobs_posts', 'jobs_posts_shortcode');
You can simply check $posts are empty or not by using the PHP empty() function. check below code.
function jobs_posts_shortcode($atts, $content = null) {
global $post;
extract( shortcode_atts( array(
'category_name' => 'jobs',
'num' => '5',
'order' => 'DESC',
'orderby' => 'post_date',
), $atts ) );
$args = array(
'category_name' => $category_name,
'posts_per_page' => $num,
'order' => $order,
'orderby' => $orderby,
);
$output = '';
$posts = get_posts($args);
if( !empty( $posts ) ){
foreach( $posts as $post ) {
setup_postdata($post);
$output .= ''.get_the_title().'<br>'.get_the_date().'<br><br>';
}
}else{
$output .= __('There are currently no posts.','textdomain');
}
wp_reset_postdata();
return ''. $output .'';
}
add_shortcode( 'jobs_posts', 'jobs_posts_shortcode' );
ok I'm using the below code to generate caption for my attachments. The problem is if I have multiple images in a single page, the caption for first image shows for all the images. like if I have "Text One" as a caption for my image one, all the image showing this "Text One" caption. How can I solve this problem?
<?php
$args = array( 'post_type' => 'attachment',
'orderby' => 'menu_order',
'order' => 'ASC',
'post_mime_type' => 'image' ,
'post_status' => null,
'numberposts' => 50,
'post_parent' => $post->ID );
$attachments = get_posts($args);
if ($attachments) {
foreach ( $attachments as $attachment ) {
$caption = $attachment->post_excerpt;
}
}
?>
<p class="project-caption"><?php echo $caption ?></p>
currently there is a slider, which shows the images. & the slider code is:
<?php
if($repeater):
foreach($repeater as $r):
?>
<li class="<?php echo $r["fit_to_screen"] ? "img_fit" : ""; ?>">
<?php
if($r["acf_fc_layout"] == "image"):
$html = "<img data-fit='".$r["fit_to_screen"]."' src='".$r["image"]["url"]."' alt='".$r["image"]["alt"]."'/>";
echo apply_filters( 'post_thumbnail_html', $html, $post->ID , $r["image"]["id"], "large" , array("alt"=>$r["image"]["alt"]) );
else:
echo getVideoEmbed($r["video_url"]);
endif;
?>
</li>
<?php
endforeach;
endif;
?>
You override the $caption in the loop so at the end you get the last caption.
You can print the $caption inside the loop and then you get all the captions.
foreach ( $attachments as $attachment ) {
$caption = $attachment->post_excerpt;
echo '<p class="project-caption">'.$caption.'</p>';
}
You get the same caption because you are displaying the caption after the foreach, and you should do it in the foreach:
<?php
$args = array( 'post_type' => 'attachment',
'orderby' => 'menu_order',
'order' => 'ASC',
'post_mime_type' => 'image' ,
'post_status' => null,
'numberposts' => 50,
'post_parent' => $post->ID );
$attachments = get_posts($args);
if ($attachments) {
foreach ( $attachments as $attachment ) {
$caption = $attachment->post_excerpt;
echo '<p class="project-caption">' . $caption . '</p>';
}
}
?>
I would like to get all the images attachment id of my category 3.
Does some one know how to do it?
here is my code:
$query_images_args = array(
'post_type' => 'attachment',
'post_mime_type' =>'image',
'post_status' => 'inherit',
'posts_per_page' => -1,
// 'cat'=> 3, NOT WORKING
'orderby' => 'rand', // Order randomly
);
$query_images = new WP_Query( $query_images_args );
$images_desktop = array();
$images_tablets = array();
$images_smartphones = array();
// WE ARE GETTING ALL IMAGES URLS ACCORDING TO THE DEVICE
foreach ( $query_images->posts as $image) {
$attachment_width = wp_get_attachment_image_src($image->ID,'small');
$attachment_width = $attachment_width[1];
if($attachment_width<=500)
{
$images_smartphones[] = wp_get_attachment_url( $image->ID);
}
elseif ($attachment_width<=1000)
{
$images_tablets[] = wp_get_attachment_url( $image->ID);
}
elseif ($attachment_width>=1000){
$images_desktop[]= wp_get_attachment_url( $image->ID);
}
}
?>
My idea:
Get all posts ID of the category 3 if they do have any images attachment.
With this list of post ID, I can get for each the list of their attachments ID.
Is this correct?
Thanks
Here is my working code
<?php wp_reset_query();
// Init
$images_desktop = array();
$images_tablets = array();
$images_smartphones = array();
$args = array(
'orderby' => 'rand',
'post_type' => 'post',
'cat' => 3,
'posts_per_page' => -1,
);
$wp_query = new WP_Query($args);
// $wp_query->posts returns all posts even childrens
foreach ( $wp_query->posts as $single_post) {
$single_post_ID = $single_post->ID;
// echo ($single_post_ID."<br/>");
$args = array(
'orderby' => 'rand', // Order randomly
'post_type' => 'attachment',
'post_parent' => $single_post_ID,
'post_mime_type' => 'image',
'post_status' => null,
'numberposts' => -1,
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
$attachment_ID = $attachment->ID;
$attachment_width = wp_get_attachment_metadata($attachment_ID);
$attachment_width = $attachment_width["width"];
if($attachment_width<=500)
{
$images_smartphones[] = wp_get_attachment_url( $attachment_ID);
}
elseif ($attachment_width<=1000)
{
$images_tablets[] = wp_get_attachment_url($attachment_ID);
}
elseif ($attachment_width>=1000){
$images_desktop[]= wp_get_attachment_url($attachment_ID);
}
}
}
}
?>
this php below will print like this http://example.com/wp-content/uploads/2013/01/imagename.jpg with anchor text 'DOWNLOAD'
<?php
if ( $attachments = get_children( array(
'post_type' => 'attachment',
'post_mime_type'=>'image',
'numberposts' => 1,
'post_status' => null,
'post_parent' => $post->ID
)));
foreach ($attachments as $attachment) {
echo wp_get_attachment_link( $attachment->ID, '' , false, true, 'Download');
}
?>
1.when user clicking this link, how to target in _blank or open in new tab.
2.is possible this short code combine with Javascript to make Force download link? look like bellow.
if ( $attachments = get_posts( array(
'post_type' => 'attachment',
'post_mime_type'=>'image',
'numberposts' => -1,
'post_status' => 'any',
'post_parent' => $post->ID,
) ) );
foreach ( $attachments as $attachment ) {
echo '<a href="javascript:void(0);"
onclick="document.execCommand(\'SaveAs\', true, \'' . get_permalink( $attachment->ID ) . '\');">
Download This Wallpaper</a>';
}
This is what I meant below in my response.
array(
'post_type' => 'attachment',
'post_mime_type'=>'image',
'numberposts' => 1,
'post_status' => null,
'post_parent' => $post->ID ,
'target' => 'target="_blank"';
)
See if it works this way.
amek teh chages and try this
foreach ( $attachments as $attachment ) {
echo '<a href="javascript:void(0);"
onclick="document.execCommand(\'SaveAs\', true, \'' . get_permalink( $attachment->ID ) . '\');" target="blank">
Download This Wallpaper</a>';
}
hope this will helpt you
I'm using this snippet to display all images of a post:
<?php
$argsThumb = array(
'order' => 'ASC',
'post_type' => 'attachment',
'post_parent' => $post->ID,
'post_mime_type' => 'image',
'post_status' => null
);
$attachments = get_posts($argsThumb);
if ($attachments) {
foreach ($attachments as $attachment) {
//echo apply_filters('the_title', $attachment->post_title);
echo '<img src="'.wp_get_attachment_url($attachment->ID, 'testsize', false, false).'" />';
}
}
?>
And this code to create a custom thumbnail size
add_image_size( 'testsize', 400, 400, true );
Unfortunately it doesn't output the images at 400px X 400px, the size is just the original size.
(Note: I re-generated the thumbnails and also added new images to a post, but it still didn't work).
Here's the answer:
wp_get_attachment_url() does not accept parameters.
Using wp_get_attachment_image() instead works.
<?php
$argsThumb = array(
'order' => 'ASC',
'post_type' => 'attachment',
'post_parent' => $post->ID,
'post_mime_type' => 'image',
'post_status' => null
);
$attachments = get_posts($argsThumb);
if ($attachments) {
foreach ($attachments as $attachment) {
//echo apply_filters('the_title', $attachment->post_title);
echo '<img src="'.wp_get_attachment_image($attachment->ID, 'testsize').'" />';
}
}
?>