WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Get the images attached to post expect the first image?

[Resolved] Get the images attached to post expect the first image?

  • Hello,

    This gets all the images attached to a post

    <?php
    	$args = array(
    	'order'          => 'ASC',
    	'orderby'        => 'menu_order',
    	'post_type'      => 'attachment',
    	'post_parent'    => $post->ID,
    	'post_mime_type' => 'image',
    	'post_status'    => null,
    	'numberposts'    => -1,
    	);
    	$attachments = get_posts($args);
    	if ($attachments) {
    	    foreach ($attachments as $attachment) {
    
    	    echo wp_get_attachment_link($attachment->ID, 'thumbnail', false, false);
    	}
    } ?>

    I get a thumbnail image for the post in the beginning of the post and then in the end of the post I get the rest of the images. Now the first thumbnail image is also added in end of the post, where it should not be anymore.

    Huge thanks for all the help!

Viewing 9 replies - 1 through 9 (of 9 total)
  • esmi

    @esmi

    Forum Moderator

    If you’re just echoing the images out using a foreach loop, then all of the images will be displayed. So what exactly are you using to display the images at the end of the post?

    I use the same snippet in front and in the end, and I would need a tad different function in the end.

    In English I want to do:

    //in beginning of the post
        SPIT OUT THE FIRST ATTACHED IMAGE (that's easy)
    //in end of the post
        SPIT OUT ALL ATTACHED IMAGES EXPECT THE FIRST (how??)

    Thanks for the reply!

    the addition of a conditional statement to your code might do the trick:

    if ($attachments) {
    $no_show=true;
    	    foreach ($attachments as $attachment) {
    if($no_show) {$no_show=false; continue; }
    	    echo wp_get_attachment_link($attachment->ID, 'thumbnail', false, false);
    	}

    Thanks for replying!

    I tried to implement it but I couldn’t wrap my head around it. Could you be more specific?

    there is no way to be more specific without sitting on your computer and doing it for you.

    there were only two lines of new code intruduced into the code that you posted. i cannot get any simpler.

    so here again, same thing, to show you what to replace inyour code:

    these are the last lines of your old code:

    $attachments = get_posts($args);
    	if ($attachments) {
    	    foreach ($attachments as $attachment) {
    
    	    echo wp_get_attachment_link($attachment->ID, 'thumbnail', false, false);
    	}
    } ?>

    replace them with:

    $attachments = get_posts($args);
      if ($attachments) {
    $no_show=true;
    	    foreach ($attachments as $attachment) {
    if($no_show) {$no_show=false; continue; }
    	    echo wp_get_attachment_link($attachment->ID, 'thumbnail', false, false);
    	}
    } ?>

    this is all.

    Sorry for that.

    It did not work, that’s why asked. It shows nothing.

    To be clear:

    <?php //GET THE FIRST IMAGE
    	$args = array(
    	'order'          => 'ASC',
    	'orderby'        => 'menu_order',
    	'post_type'      => 'attachment',
    	'post_parent'    => $post->ID,
    	'post_mime_type' => 'image',
    	'post_status'    => null,
    	'numberposts'    => -1,
    	);
    	$attachments = get_posts($args);
      		if ($attachments) {
    	$no_show=true;
    			foreach ($attachments as $attachment) {
    	if($no_show) {$no_show=false; continue; }
    			echo wp_get_attachment_link($attachment->ID, 'thumbnail', false, false);
    	}
    } ?>
    
    // CONTENT AND OTHER STUFF...
    
    <?php //GET ALL EXPECT FIRST IMAGE
    	$args = array(
    	'order'          => 'ASC',
    	'orderby'        => 'menu_order',
    	'post_type'      => 'attachment',
    	'post_parent'    => $post->ID,
    	'post_mime_type' => 'image',
    	'post_status'    => null,
    	'numberposts'    => -1,
    	);
    	$attachments = get_posts($args);
      		if ($attachments) {
    	$no_show=true;
    			foreach ($attachments as $attachment) {
    	if($no_show) {$no_show=false; continue; }
    			echo wp_get_attachment_link($attachment->ID, 'thumbnail', false, false);
    	}
    } ?>

    Huge thanks again!

    the code has to be somewhere in the loop or somewhere where $post->IDis defined.

    try to echo $post->IDat the beginning of your code to see if it works.

    a small adjustment to the first part – to show the first image –
    (set numberpost=1, and no if( $no_show) there):

    <?php //GET THE FIRST IMAGE
    	$args = array(
    	'order'          => 'ASC',
    	'orderby'        => 'menu_order',
    	'post_type'      => 'attachment',
    	'post_parent'    => $post->ID,
    	'post_mime_type' => 'image',
    	'post_status'    => null,
    	'numberposts'    => 1,
    	);
    	$attachments = get_posts($args);
      		if ($attachments) {
    
    			foreach ($attachments as $attachment) {
    
    			echo wp_get_attachment_link($attachment->ID, 'thumbnail', false, false);
    	}
    } ?>
    
    // CONTENT AND OTHER STUFF...
    
    <?php //GET ALL EXPECT FIRST IMAGE
    	$args = array(
    	'order'          => 'ASC',
    	'orderby'        => 'menu_order',
    	'post_type'      => 'attachment',
    	'post_parent'    => $post->ID,
    	'post_mime_type' => 'image',
    	'post_status'    => null,
    	'numberposts'    => -1,
    	);
    	$attachments = get_posts($args);
      		if ($attachments) {
    	$no_show=true;
    			foreach ($attachments as $attachment) {
    	if($no_show) {$no_show=false; continue; }
    			echo wp_get_attachment_link($attachment->ID, 'thumbnail', false, false);
    	}
    } ?>

    i tried it in my test setup in single.php, and it worked as intended.

    It works! Thank you so much!!

    my images are not showing up in my posts after uploading them.
    It was working until June to present. I had deactivated Next Gallery 2 days ago because I was getting frustated, and thinking that was the problem? Can someone help me.

    http://accentsofhome.com
    I have posted this 2 times and no one answers.

    Thanks,
    Kathy allen
    email: uniquebiz@hotmail.com

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘[Resolved] Get the images attached to post expect the first image?’ is closed to new replies.
Skip to toolbar