WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Get the images attached to post expect the first image? (10 posts)

  1. hilj
    Member
    Posted 4 years ago #

    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!

  2. esmi
    Forum Moderator
    Posted 4 years ago #

    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?

  3. hilj
    Member
    Posted 4 years ago #

    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!

  4. alchymyth
    Forum Moderator
    Posted 4 years ago #

    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);
    	}
  5. hilj
    Member
    Posted 4 years ago #

    Thanks for replying!

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

  6. alchymyth
    Forum Moderator
    Posted 4 years ago #

    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.

  7. hilj
    Member
    Posted 4 years ago #

    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!

  8. alchymyth
    Forum Moderator
    Posted 4 years ago #

    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.

  9. hilj
    Member
    Posted 4 years ago #

    It works! Thank you so much!!

  10. kapallen
    Member
    Posted 4 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic