不用任何插件实现WordPress文章点击数

如果你是用WordPress搭建的网站,你是不是想知道你的文章被多少人点击看过?一些用于统计点击次数的插件可以解决这个问题。不过,今天要推荐的是一个更简单的不用任何插件实现点击数统计的方法。首先,需要创建相关的函数。你可以把下面的代码粘贴到你所用主题的 functions.php 文件里。

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 View";
    }
    return $count.' Views';
}
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);
    }
}

然后,粘贴下面的代码到主题的 single.php 的文章的 loop 里:

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

最后,把下面的代码拷贝到任意你的主题模板里想要显示点击数的地方:

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


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
中文名: WordPress 宝典 原名: WordPress Bible 别名: WordPress,Bible,宝典,PHP,CMS 作者: Aaron Brazell资源格式: PDF 出版社: Wiley书号: 978-0-470-56813-2发行时间: 2010年 地区: 美国 语言: 英文 简介:   WordPress 是一种使用 PHP 语言和 MySQL 数据库开发的开源、免费的 Blog(博客,网志)引擎,用户可以在支持 PHP 和 MySQL 数据库的服务器上建立自己的 Blog。   WordPress 是一个功能非常强大的博客系统,件众多,易于扩充功能。安装和使用都非常方便。目前 WordPress 已经成为主流的 Blog 搭建平台。   WordPress 的原版是英文版的,UTF-8 编码,最新版本为 2.9。为满足日益庞大的中文用户需求开辟了中文官方站点且提供中文版程序下载。还有爱好者开发了中文语言包,使其可以支持中文。不过,由于使用的编码原因,中文字符截断时会出现乱码。不过桑葚网友制作的中文 WordPress 工具箱件可以解决这个问题。   WordPress 可以说是世界上目前最先进的 weblog 程序。目前开发的程序大多都是根据它仿造的。它更能把握搜索引擎,在你使用 WordPress 并掌握几种件后,对于优化将不用过多的操心,它会为你想的更多。   WordPress有许多第三方开发的免费模板,安装方式简单易用。不过要做一个自己的模板,则需要你有一定的专业知识。比如你至少要懂的HTML代码、CSS、PHP等相关知识。 Install WordPress and go beyond blogging WordPress is so flexible that developers are now tapping it to create robust applications for content, contact, and e-mail management. Whether you're a casual blogger or programming pro, this comprehensive guide covers WordPress from the basics through advanced application development. Learn how to use custom plugins and themes, retrieve data, maintain security, use social media, and modify your blog without changing any core code. You'll even get to know the ecosystem of products that surrounds this popular, open-source tool. Enhance your blog\'s findability in the search engines and beyond Discover hooks and leverage the WordPress event-driven programming interface Create WordPress widgets in only a few minutes Explore alternate uses of WordPress Enhance your blog with WordPress MU Ensure your plugins maintain future compatibility Create highly customizable and dynamic themes using template tags Learn best security practices as a user and developer Companion Web Site Visit www.wiley.com/go/wordpressbible for all of the author's example files from the book. Install, secure, and maintain WordPress Extend WordPress with plugins and themes Enhance your blog with WordPress MU 目录: Credits About the Author About the Technical Editor Contents Preface WordPress Versioning Getting the Most Out of This Book Using the Margin Icons Part I: Getting Started with WordPress Chapter 1: Learning About WordPress A Brief History of WordPress Leveraging the WordPress Community WordPress by the Numbers 10 Things You Need to Know About WordPress Understanding Open Source and the General Public License Summary Chapter 2: Installing and Configuring WordPress System Requirements Choosing a Web Server Installing WordPress Summary Chapter 3: WordPress, SEO, and Social Media Marketing Understanding the Basics of Search Engine Optimization Leveraging Social Networks to Extend Your Blog Summary Chapter 4: Finding Help in the WordPress Support System Using the WordPress Codex Staying in the Loop with Mailing Lists Finding Help in the WordPress Support Forums Summary Part II: Working with Plugins Chapter 5: Extending WordPress with Plugins Understanding WordPress Hooks Writing Your Own Plugin Extending the WordPress Admin Creating Events with Actions Modifying Content with Filters Using Multi-Argument Hooks Localizing Plugins Summary Chapter 6: Widgetizing WordPress Using Widgets in WordPress Building Widgets with the Widget API Widgetizing Your Theme Summary Chapter 7: Understanding the WordPress Database Class Examining the Schema and Properties of the Database Class Adding Data to MySQL with WordPress Retrieving Data from MySQL with WordPress Preventing SQL Injection Summary Chapter 8: Dissecting the Loop and WP_ Query Defining the Loop Wrangling the Loop with Plugins Developing Custom and Multiple Loops Using Loops Strategically Summary Part III: Working with Themes and Template Tags Chapter 9: Using Free or Premium Themes Understanding the User Experience Finding and Implementing Free Themes Finding and Implementing Premium Themes Summary Chapter 10: Understanding the Template File Hierarchy Looking at the Minimum Necessary Template Files Understanding the Common Template Files Enhancing the User Experience with Template Files Developing Custom Template Files Summary Chapter 11: Adding JavaScript and CSS to Themes Examining the jQuery and Prototype Frameworks Leveraging WordPress’ JavaScript Libraries Examining WordPress Plugins that Use JavaScript Effects Looking at Theme Styles Summary Chapter 12: Dissecting the Comment Loop, Template Tags, and Theme Best Practices Using Hooks in Themes Implementing Scripts in Themes Using Template Tags to Make Themes Dynamic Creating Conversations with Threaded Comments and Paged Comments Personalizing the Reader Experience with Avatars Summary Part IV: Creating Content Chapter 13: Navigating the Content Production Experience Customizing Your Workspace Leveraging the Elements of Content Creation Hacking Your Experience: Getting the Most Out of Writing Summary Chapter 14: Using Offline Editors What Is XML-RPC? Understanding XML-RPC Security Risks Looking at Offline Blog Editors Summary Part V: Keeping Up with the Joneses: Maintenance and Upgrades Chapter 15: Performing Automatic Upgrades Meeting the Minimum Automatic Upgrade Requirements Disabling the “Upgrade Nag” Performing Automatic Upgrades Using SSH for Automatic Upgrades Summary Chapter 16: Moving to WordPress and Backing It Up Moving a Blog to WordPress Importing Tags into WordPress Looking at Backup Routines Summary Chapter 17: WordPress Maintenance and Security Upgrading WordPress Choosing an FTP Client Practicing Sound WordPress Security Summary Chapter 18: Caching Strategy to Ensure WordPress Scales Understanding Caching Using WordPress Caching Mechanisms Optimizing PHP with Opcode Caching Caching MySQL with HyperDB and the Query Cache Harnessing the “Cloud” Summary Chapter 19: Understanding WordPress Roles and Capabilities Looking at WordPress Roles and Capabilities Using the Role Manager Plugin Summary Part VI: Alternate Uses for WordPress Chapter 20: Using WordPress for Alternative Blogging Photoblogging with WordPress Implementing a Twitter-style Blog Using Press This for a Tumble Blog Summary Chapter 21: WordPress as a Content Management System Using WordPress as a Content Management System Understanding Enterprise WordPress Needs Conveying a Consistent Message and Brand Understanding When a Blog Is Not a Blog Summary Part VII: Looking at the WordPress Ecosystem Chapter 22: Leveraging WordPress MU and Multi-Blog Functionality Installing and Configuring WordPress MU Converting a WordPress Blog to WordPress MU Understanding WordPress MU Plugin Nuances Adapting to WordPress MU Summary Chapter 23: Adding User Forums with bbPress Installing bbPress Finding bbPress Plugins Understanding the bbPress Theme System Summary Chapter 24: Creating Your Own Social Network with BuddyPress What Is BuddyPress? Configuring BuddyPress Comparing BuddyPress and WordPress Development Looking at BuddyPress Theme Concepts Extending BuddyPress Summary Chapter 25: Using BackPress as a Development Framework Defining BackPress Developing with BackPress Solving BackPress Dependencies Summary Chapter 26: WordPress.com and the Automattic Products About Automattic Taking a Look at Automattic’s Propriety Products Using WordPress.com Themes Getting Your Plugin Included in WordPress.com Buying Premium WordPress.com Features Summary Chapter 27: Leveraging Automattic Products Obtaining a WordPress.com API Key Using Akismet to Kill Spam Making Use of WordPress.com Stats Engaging Readers with IntenseDebate Crowdsourcing with PollDaddy Summary Part VIII: Appendixes Appendix A: WordPress Hook Reference Using Actions Using Filters Hook Reference: Alphabetical Listing Hook Reference: General Uses Summary Appendix B: Template Tags Understanding the Template Tag Concept Breaking Down the WordPress Templating System Summary Appendix C: What About PHP 5? PHP 4 versus PHP 5: Understanding Core Differences WordPress and PHP 5 Summary Appendix D: WordPress Hosting Features and Requirements of Web Hosts Types of Web Hosts Summary Appendix E: WordPress Vendors and Professional Services Top Consultants in the WordPress Community Directory of Automattic-Recommended Consultants Summary Appendix F: WordPress in Government WordPress Use in the Federal Government WordPress Use Outside the United States Summary Appendix G: WordPress in Major Media Newsroom Challenges for WordPress Roadmapping WordPress in Major Media Summary Appendix H: The General Public License Preamble GNU GENERAL PUBLIC LICENSE Index

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值