无插件实现WordPress调用网站中浏览量最多文章

鱼见海资源网最新教程:

使用Wordpress程序建网站,有进需要在侧边栏调用网站中浏览量最多的文章列表。以前为了实现这个功能,我们需要安装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'=>10,

'order' => 'DESC'

);

query_posts($args);  while (have_posts()) : the_post();?>

  • 文章来源于互联网

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

    昵称

    取消
    昵称表情代码图片

      暂无评论内容