有问题得到准确的后期图像

时间:2011-08-15 13:38:18

标签: php wordpress

现在我尝试了其他一些方法......

<?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 href="' . 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 */ ?>

和它的工作,但它只显示最近的帖子。除非最热门的帖子:(

.....

遇到热门帖子的问题......

<?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 */ ?>

我想抓住3个热门帖子,但是当我使用上面的代码时,他们只是向我展示了我最近发布的帖子......

帮我弄清楚这个

2 个答案:

答案 0 :(得分:2)

尝试使用get_the_post_thumbnail( $id, $size, $attr )并传入帖子ID。另请注意,您应在查询post_type = 'post'中指定过滤掉非帖子。

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

-

我已在本地确认以下工作:

// 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 );

    }

}

答案 1 :(得分:0)

函数the_post_thumbnail()将检索循环中当前帖子的后缩略图。要获取任何帖子ID的帖子缩略图,请使用:

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