川贝小屋
  • Chat Now
川贝小屋
  • Blog
  • Blog Details
  • Service Details
  • Project Details
  • Address

    California, TX 70240
  • Email

    support@validtheme.com
  • Contact

    +44-20-7328-4499

Get Subscribed!

WordPress获取文章浏览次数(亲测)

  • Home
  • WordPress
  • WordPress获取文章浏览次数(亲测)
wordpress浏览次数
  • 2024年9月7日
  • 川贝
  • 859 Views

在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() ); ?>
打赏赞微海报分享

Tags:

WordPress主题开发 WordPress浏览次数 WordPress调用函数 WP主题开发

Share:

Previus Post
Wo
Next Post
Pa

Comments are closed

近期文章

  • 为什么现在很少有企业再提ISO体系认证了?
  • 根服务器是什么?
  • SEO 英文术语大全
  • 阿里云服务器一个网站要占40G空间?
  • PayPal如何取消自动扣款(自动付款)

近期评论

您尚未收到任何评论。

归档

  • 2024 年 9 月
  • 2024 年 8 月
  • 2024 年 3 月

分类目录

  • Business
  • English
  • Learning
  • WordPress

Recent Posts

  • 为什么现在很少有企业再提ISO体系认证了?
  • 根服务器是什么?
  • SEO 英文术语大全
  • 阿里云服务器一个网站要占40G空间?
  • PayPal如何取消自动扣款(自动付款)

Recent Comments

您尚未收到任何评论。

Archives

  • 2024 年 9 月
  • 2024 年 8 月
  • 2024 年 3 月

Categories

  • Business
  • English
  • Learning
  • WordPress

Tags Cloud

DNS系统 ICANN ISO体系认证 ISO国际认证 PayPal PayPal Autopay PayPal取消自动付款 Paypal取消自动扣款 TLD WordPress WordPress主题开发 WordPress当页链接 WordPress文章链接 WordPress本文链接 WordPress浏览次数 WordPress调用函数 WP主题开发 会展中英文对照 会展英语 会展英语翻译 服务器空间占满 根服务器 阿里云服务器 顶级域

Categories

  • Business (3)
  • English (2)
  • Learning (1)
  • WordPress (2)

Recent Posts

  • 为什么现在很少有企业再提ISO体系认证了?缩略图
    14 9 月, 2024为什么现
  • dns root server
    08 9 月, 2024根服务器
  • seo英文术语
    08 9 月, 2024SEO
  • 阿里云服务器一个网站要占40G空间?缩略图
    08 9 月, 2024阿里云服

Tags

DNS系统 ICANN ISO体系认证 ISO国际认证 PayPal PayPal Autopay PayPal取消自动付款 Paypal取消自动扣款 TLD WordPress WordPress主题开发 WordPress当页链接 WordPress文章链接 WordPress本文链接 WordPress浏览次数 WordPress调用函数 WP主题开发 会展中英文对照 会展英语 会展英语翻译 服务器空间占满 根服务器 阿里云服务器 顶级域

Copyright 2011-2025 LIXINGFENG All Rights Reserved by LIXINGFENG.COM

  • Light
  • Contact