1

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);
    if($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>';
        }
    }
    return $thumblist;
}

to display

then how can i limit the output of images displayed

1
  • 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.
    – user4315
    Sep 26, 2012 at 7:05

1 Answer 1

0
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>';
                    $i++;
                    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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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