WordPress增加文章浏览量配置

内容纲要

方式一:

wordpress免插件显示文章浏览量次数
https://www.xuewangzhan.net/wpbbs/3502.html

1.首先在网站后台的wordpress模板函数functions.php文件中加入以下的代码:

/*显示文章浏览次数*/
function getPostViews($postID){
    $count = get_post_meta($postID,'views', true);
    if($count==''){
        delete_post_meta($postID,'views');
        add_post_meta($postID,'views', '0');
        return "0";
    }
    return $count.'';
}
function setPostViews($postID) {
    $count = get_post_meta($postID,'views', true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID,'views');
        add_post_meta($postID,'views', '0');
    }else{
        $count++;
        update_post_meta($postID,'views', $count);
    }
}
  1. 在需要显示浏览量的地方,包括首页,分类页,文章页都可以使用。(如果对wordpress模板不了解,请先学习一下wordpress模板制作教程)添加浏览量调用代码:
    <?php setPostViews(get_the_ID()); echo number_format(getPostViews(get_the_ID())); ?>

    简单的二步,就可以实现在自己建网站时显示文章的浏览量了,每刷新一下,浏览量自动的增加一次。

软件工程师~

Leave a Comment

您的电子邮箱地址不会被公开。 必填项已用*标注

close
arrow_upward