分类、标签页面我觉得还是很有必要添加一个浏览数统计,毕竟文章多的时候,分类、标签页面也是一个很好的索引和入口,有一个访问量的数据可供显示一个非常不错的选择。实现方法很简单,只需要将下面的代码加到当前主题的 functions.php 文件最好一个 ?> 的签名即可:
- function fa_get_tax_views( $term_id = null){
- if ( !$term_id ) {
- $term = get_queried_object();
- $term_id = $term->term_id;
- }
- if ( !$term_id ) return;
- $view = get_term_meta( $term_id , '_views' , true ) ? get_term_meta( $term_id , '_views' , true ) : 0;
- return $view;
- }
- function fa_set_tax_views(){
- if ( !is_category() && !is_tag() ) return;
- $term = get_queried_object();
- $term_id = $term->term_id;
- $view = fa_get_tax_views($term_id);
- update_term_meta( $term_id, '_views' , $view + 1 );
- }
- add_action('get_header', 'fa_set_tax_views');
调用非常简单,在分类或者标签页面,直接使用以下代码调用即可:
- <?php echo fa_get_tax_views();?>
PS:如果在其他地方,在函数中传入 term_id 参数即可。
Fatesinger 声明:付费主题使用本文内容必须在主题和文章中都注明出处并附上链接。
答案内容来自Fatesinger
如若转载,请注明出处:https://www.ozabc.com/jianzhan/261933.html