开发者

having problem to get accurate post image

Now i tried some other ways....

<?php /* Start popular Post */ ?>   
    <li>
        <h3>Popular Posts</h3>
        <ul class="bullets">
        <?php
          $args = array( 'numberposts' => 5 );
          $thumbnails = get_posts($args);
          foreach ($thumbnails as $thumbnail) {
            if ( has_post_thumbnail($thumbnail->ID)) {
              echo '<a hre开发者_运维问答f="' . get_permalink( $thumbnail->ID ) . '" title="' . esc_attr( $thumbnail->post_title ) . '">';
              echo get_the_post_thumbnail($thumbnail->ID, 'thumbnail');
              echo '</a>';
            }
          }
        ?>

        </ul>
    </li>
<?php /* End popular Post */ ?>

and its working but its only showing the most recent posts. unless most popular post :(

.....

having trouble with popular post...

<?php /* Start popular Post */ ?>   
    <li>
        <h3>Popular Posts</h3>
        <ul class="bullets">
        <?php
            $popular_posts = $wpdb->get_results("SELECT id,post_title FROM {$wpdb->prefix}posts ORDER BY comment_count DESC LIMIT 0,3");
                foreach($popular_posts as $pop) {
                    if ( has_post_thumbnail($pop->ID) ) {
        ?>
            <li>
        <?php   the_post_thumbnail(array(100,100)); ?>
            </li>
        <?php
                    }
                }
        ?>
        </ul>
    </li>
<?php /* End popular Post */ ?>

i am trying to grab 3 popular posts but when i am using the above code they are just showing me image of my recent post ...

help me to figure out this


Try using get_the_post_thumbnail( $id, $size, $attr ) and pass in the Post ID. Also note that you should specify in your query post_type = 'post' to filter out non-posts.

Documentation: http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail

--

I've confirmed locally that the following works:

// Query should select only posts, order by comment_count descending
$p_posts_query = "SELECT ID, post_title 
                  FROM wp_posts 
                  WHERE post_type = 'post' 
                  ORDER BY comment_count DESC";

// Load results of our query into variable $p_posts
$p_posts = $wpdb->get_results($p_posts_query); 

// Cycle through each result
foreach ( $popular_posts as $pop ) {

    // If there is a thumbnail associated with this post
    if ( has_post_thumbnail( $pop->ID ) ) {

      // Show the thumbnail for this post
      echo get_the_post_thumbnail( $pop->ID );

    }

}


The function the_post_thumbnail() will retrieve the post thumbnail of the current post in the loop. To get a post thumbnail of any post ID, use this:

http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜