顶视图帖子没有显示与wordpress中的特色图像

时间:2015-04-16 20:27:51

标签: php wordpress post

我试图用wordpress中的sidebar.php显示我观看次数最多的帖子和特色图片。

我尝试了以下代码,但它重复显示一个特色图像三次。

这是一个link

我的代码是:

siderbar.php

<div class="popular">
<h2>Most Popular Posts</h2>

    <?php echo popularPosts(3); ?>

</div>   

function.php

<?php

function popularPosts($num) {
global $wpdb;

$posts = $wpdb->get_results("SELECT comment_count, ID, post_title FROM   $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , $num");

foreach ($posts as $post) {
    setup_postdata($post);
    $id = $post->ID;
    $title = $post->post_title;
    $count = $post->comment_count;

    if ($count != 0) {
        $popular .= '<li>';
        $popular .= '<a href="' . get_permalink($id) . '" title="' . $title . '">' . $title .the_post_thumbnail(). '</a> ';
        $popular .= '</li>';
    }
}
return $popular;
   }

   ?>

我已使用以下代码计算我的帖子视图。

 function bac_PostViews($post_ID) {

     //Set the name of the Posts Custom Field.
     $count_key = 'post_views_count'; 

     //Returns values of the custom field with the specified key from the specified post.
     $count = get_post_meta($post_ID, $count_key, true);

     //If the the Post Custom Field value is empty. 
     if($count == '')
     {
          $count = 0; // set the counter to zero.

          //Delete all custom fields with the specified key from the specified post. 
          delete_post_meta($post_ID, $count_key);

          //Add a custom (meta) field (Name/value)to the specified post.
          add_post_meta($post_ID, $count_key, '0');
          return $count . ' View';

          //If the the Post Custom Field value is NOT empty.
     }else{
          $count++; //increment the counter by 1.
          //Update the value of an existing meta key (custom field) for the specified post.
          update_post_meta($post_ID, $count_key, $count);

          //If statement, is just to have the singular form 'View' for the value '1'
          if($count == '1')
          {  
               return $count . ' View';
          }
          //In all other cases return (count) Views
          else 
          {
               return $count . ' Views';
          }
       }
    }
?>

请建议我如何展示三个带有特色图片和帖子标题的最常见的页面。

1 个答案:

答案 0 :(得分:-1)

尝试wordpress插件:

https://wordpress.org/plugins/wordpress-popular-posts/

哪个可能会帮助你.. !!!