zoukankan      html  css  js  c++  java
  • WordPress主题开发:设置和获取浏览次数

    将以下代码放在functions.php,一个是获取阅读量,一个是设置阅读量

    <?php
    
    /**
    * getPostViews()函数
    * 功能:获取阅读数量
    * 在需要显示浏览次数的位置,调用此函数
    * @Param object|int $postID   文章的id
    * @Return string $count          文章阅读数量
    */
    function getPostViews( $postID ) {
         $count_key = 'post_views_count';
         $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;
     }
    
    
    /**
    * setPostViews()函数  
    * 功能:设置或更新阅读数量
    * 在内容页(single.php,或page.php )调用此函数
    * @Param object|int $postID   文章的id
    * @Return string $count          文章阅读数量
    */
     function setPostViews( $postID ) {
         $count_key = 'post_views_count';
         $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 );
         }
     }
    
    ?>

     注意:调用了setPostViews函数后,每刷新一次就会增加一次浏览量。

    在内容页(single.php,或page.php )尝试一下吧:

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

    参考文档:

    http://wp-snippets.com/post-views-without-plugin/

  • 相关阅读:
    Selenium中的几种等待方式,需特别注意implicitlyWait的用法
    在chrome下的文本框sendkeys,提示element can't focus--解决方法
    selenium 切换窗口 每次成功code
    xpath实例 --//span[contains(.,'资讯管理')]
    SQL 常用语句
    TestNG教程
    ant常用命令
    win server服务安装
    又一次受启发
    firefox安装firebugXPath Checker
  • 原文地址:https://www.cnblogs.com/tinyphp/p/6366022.html
Copyright © 2011-2022 走看看