WordPress网站制作热门文章排行榜(浏览量排序)

鱼见海资源网最新教程:

使用Wordpress做网站,有时需要在网站的侧边栏调用热门文章,我们经常是按评论数进行排序的,今天介绍一下如何调用以浏览量排序的热门文章的方法。

方法/步骤

  1. 在自己的WORDPRESS网站模板函数文件functions.php中添加以下的浏览量函数代码;
/*文章浏览量*/
function record_visitors()  
{  
    if (is_singular())  
    {  
      global $post;  
      $post_ID = $post->ID;  
      if($post_ID)  
      {  
          $post_views = (int)get_post_meta($post_ID, 'views', true);  
          if(!update_post_meta($post_ID, 'views', ($post_views+1)))  
          {  
            add_post_meta($post_ID, 'views', 1, true);  
          }  
      }  
    }  
}  
add_action('wp_head', 'record_visitors');  
/// 函数名称:post_views  
/// 函数作用:取得文章的阅读次数  
function post_views($before = '(点击 ', $after = ' 次)', $echo = 1)  
{  
  global $post;  
  $post_ID = $post->ID;  
  $views = (int)get_post_meta($post_ID, 'views', true);  
  if ($echo) echo $before, number_format($views), $after;  
  else return $views;  
}

2. 在需要调用按浏览量排序的热门文章位置,使用以下的代码进行调用文章列表;

    'post_views_count', 'orderby' => 'meta_value_num', 'posts_per_page'=>6, 'order' => 'DESC' ); query_posts($args); while (have_posts()) : the_post();?>
  • 浏览:

3. 这样就可以调用出用户浏览最多的6篇文章了。

文章来源于互联网

© 版权声明
THE END
喜欢就支持一下吧
点赞370 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容