小冬SEO

wordpress调用文档的阅读量浏览量方法

2019-09-16 19:42:08 2491 WordPress笔记

用惯了dedecms,一时半会使用wordpress程序建站,什么都得百度,不过还好网上关于wordpress程序的教程有很多,比如调用文章的阅读量浏览量,我就找到了一下方法:

找到主题模板中的文件:functions.php

在文件中加入如下代码:

//如何获得WordPress文章浏览次数的统计

function getPostViews($postID){

    $count_key = 'post_views';

    $count = get_post_meta($postID, $count_key, true);

    if($count==''){

        delete_post_meta($postID, $count_key);

        add_post_meta($postID, $count_key, '0');

        return "0";

    }

    return $count.'';

}

function setPostViews($postID) {

    $count_key = 'post_views';

    $count = get_post_meta($postID, $count_key, true);

    if($count==''){

        $count = 0;

        delete_post_meta($postID, $count_key);

        add_post_meta($postID, $count_key, '0');

    }else{

        $count++;

        update_post_meta($postID, $count_key, $count);

    }

} 

然后列表页通过以下代码调用浏览量:

<?php echo getPostViews(get_the_ID()); ?>

然后内容页面通过以下代码调用:

<?php? setPostViews(get_the_ID()); ?><?php echo getPostViews(get_the_ID()); ?>

说明:该调用方式是统计文章被打开过多少次

版权保护: 本文由小冬SEO编辑发布,转载请保留链接: http://www.myseoyh.cn/shuo/107.html