Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

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. –  Michał Gancarski Sep 26 '12 at 7:05

1 Answer 1

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

share|improve this answer

Your Answer

 
discard

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.