• I have tried several attempts to do this but I have failed.
    I have tried to setup a custom page template and on that template I want to display the last image attached to my latest post.
    I found how to attach the last post to the page but I would like just the picture.
    Can someone help?

Viewing 6 replies - 1 through 6 (of 6 total)
  • <?php query_posts('posts_per_page=1');
    if ( have_posts() ) : while ( have_posts() ) : the_post();
    $images =& get_children( 'post_type=attachment&post_mime_type=image' );
    if ( !empty($images) ) {
    	$last_image = count( $images) - 1;
    	echo wp_get_attachment_image( $images[$last_image], 'full' );
    	}
    }
     ..
    endwhile; else:
     ..
    endif;
    ?>

    [WARNING: untested]

    Thread Starter jmckay1

    (@jmckay1)

    Thanks!
    I tried it and I got a server 500 error on the website.

    My code isn’t normally that bad! You do realise that ... bits were intended as place markers for the rest of your Loop, yes?

    Thread Starter jmckay1

    (@jmckay1)

    sorry I’m still new to wordpress and templates and how it does loops. I am familiar with php I would not say I’m a pro!
    So I think I understand what your saying now.
    I do have a question in your script you posted-
    echo wp_get_attachment_image( $images[$last_image], 'full' );
    So if the last post is not empty and has a image it should display the image correct?

    I could be reading it backwards.

    Close. The code tries to grab all image attachments that are associated with the post and then displays the last image.

    Thread Starter jmckay1

    (@jmckay1)

    I’m sorry time got away from me! I got what you where saying and got it to work Thank you for your help!!!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘display last pic from post.’ is closed to new replies.