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

方法/步骤
- 在自己的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. 在需要调用按浏览量排序的热门文章位置,使用以下的代码进行调用文章列表;
3. 这样就可以调用出用户浏览最多的6篇文章了。
文章来源于互联网
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容