Support » Fixing WordPress » Using post thumbnails outside the loop

  • Hey everyone. What I’m trying to do is use the new post thumbnails feature in conjunction with the WordPress.com Stats plugin. I’m using this code to grab the most popular posts and display them on one of my pages:

    <h4>Most Popular</h4>
    <ul>
    <?php foreach($top_posts = stats_get_csv('postviews', "days=7") as $post): if(!get_post($post['post_id'])) continue; ?>
    	<li><a href="<?php echo $post['post_permalink']; ?>"><?php echo $post['post_title']; ?></a></li>
    
    <?php endforeach; ?>

    That is working and displaying the posts perfectly fine. However, what I’m trying to do is represent each post by a thumbnail instead of the post title. I’ve tried squeezing <?php the_post_thumbnail(); ?> in there, but it doesn’t seem to be working.

    Is this even possible to do? Any help is appreciated.

Viewing 7 replies - 1 through 7 (of 7 total)
  • If anyone could help me set up a custom WP query that would basically do the same thing using WordPress.com stats, that would be even better.

    You’ll need to pass the post id into the thumbnail call.

    <?php the_post_thumbnail( (int) $post['post_id'] ); ?>

    Could either of you explain this in a little more detail? Sorry, I don’t really understand PHP too well.

    t31os_, is this what you were talking about?

    <h4>Most Popular</h4>
    
    <?php foreach($top_posts = stats_get_csv('postviews', "days=7") as $post): if(!get_post($post['post_id'])) continue; ?>
    	<a href="<?php echo $post['post_permalink']; ?>"><?php the_post_thumbnail( (int) $post['post_id'] ); ?></a>
    
    <?php endforeach; ?>

    Yes, that’s what i meant, but it’s not possible anyway, having checked…

    You’ll need to use this instead..

    <?php echo get_the_post_thumbnail( (int) $post['post_id'] ); ?>

    Is there any way to display all posts thumbnails outside the loop? I need to create some kind of a gallery with posts thumbnails so i need to display them all together

    I managed to display all attachments via code

    <?php
    $args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => null );
    $attachments = get_posts( $args );
    if ($attachments) {
    	foreach ( $attachments as $post ) {
    		setup_postdata($post);
    		the_title();
    		the_attachment_link($post->ID, false);
    		the_excerpt();
    	}
    }
    ?>

    That displayed all the attachments including featured images. So how to exclude now any other types of attachments and leave only the featured images?

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Using post thumbnails outside the loop’ is closed to new replies.