Home > Backend Development > PHP Tutorial > wordpress怎么添加文章阅读数量

wordpress怎么添加文章阅读数量

PHPz
Release: 2020-09-04 15:08:03
Original
2066 people have browsed it

wordpress怎么添加文章阅读数量

wordpress添加文章阅读数量的方法:

将下面代码添加到functions.php

//取得文章的阅读次数  
  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;  
  }  
 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');
Copy after login

然后在需要用到的地方加入下面的代码即可

<?php post_views(); ?>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template