WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can i show the last 2 post attachments in my category template?

I tried the following

But i need to show the last 2 post image attachments

if i have the following code

function show_all_thumbs() {
    global $post;
    $post = get_post($post);
    /* image code */
    $images =& get_children( 'post_type=attachment&post_mime_type=image&output=ARRAY_N&orderby=menu_order&order=ASC&post_parent='.$post->post_parent);
        foreach( $images as $imageID => $imagePost ){
            $the_b_img = wp_get_attachment_image($imageID, 'thumbnail', false);
            $thumblist .= '<a href="'.get_attachment_link($imageID).'">'.$the_b_img.'</a>';
    return $thumblist;

to display

then how can i limit the output of images displayed

share|improve this question
I was going to downvote this question but instead I will just suggest looking here: php.net/manual/en/ref.array.php Hint: you have an array of results. You want to obtain last two members of the array. – wssbck Sep 26 '12 at 7:05
function show_all_thumbs() {
            global $post;
            $post = get_post( $post );
            /* image code */
            $images = & get_children( 'post_type=attachment&post_mime_type=image&output=ARRAY_N&orderby=menu_order&order=ASC&post_parent=' . $post->post_parent );
            if( $images ) {
                $i = 0;
                $images = array_reverse( $images );
                foreach( $images as $imageID => $imagePost ) {
                    unset( $the_b_img );
                    $the_b_img = wp_get_attachment_image( $imageID, 'thumbnail', false );
                    $thumblist .= '<a href="' . get_attachment_link( $imageID ) . '">' . $the_b_img . '</a>';
                    if( 2 == $i ) { break; }
            return $thumblist;

Use that. Or, use what I wrote above but take out the $images = array_reverse( $images ); line and change &order=ASC to &order=DESC

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.