首页 WordPress 简易信息统计(代码)

WordPress 简易信息统计(代码)

chenmo 2013-2-26 2 1835

    提到 WordPress,许多使用它来搭建的博客或网站都喜欢向用户显示一些网站统计数据,比如日志、评论、分类和用户等各自的总数,意义虽然并不是很大,但毕竟可以让读者/用户对自己的网站概况有一定的了解,因此,从这个角度来看也不是没有必要的。

  其实,WordPress 已经内置有以上提及的这些数据统计功能,我们可以登录后台查看,但也仅限于管理员本人才拥有这个权限。而写作本文的初衷是向读者显示这些统计数据的,所以,应该将这些数据摆在前台。

  WordPress 有许多实现这种数据统计功能的插件,但如果仅仅想要显示以上这几项的统计数据,似乎并没有必要安装任何的插件,我们可以只使用一段简单的代码就可以实事想要的效果。

  下面的这段代码来自Deluxeblogtips,我照抄过来了,为了方便中文用户理解和使用,我将其中的几个英文名称改成了中文。好了,将这段代码添加到当前主题的 functions.php 文件中:

function simple_stats() {
global $wpdb;
$stats = array();
$stats['posts'] = number_format_i18n(wp_count_posts('post')->publish);
$stats['pages'] = number_format_i18n(wp_count_posts('page')->publish);
$stats['cats']  = number_format_i18n(wp_count_terms('category'));
$stats['tags'] = number_format_i18n(wp_count_terms('post_tag'));
$stats['comments'] = number_format_i18n(wp_count_comments()->approved);
$stats['users'] = $wpdb->get_var("SELECT COUNT(ID) FROM {$wpdb->prefix}users");
echo '<div class="simple-stats">',
'<p>日志总数: <b>', $stats['posts'], '</b></p>',
'<p>页面总数: <b>', $stats['pages'], '</b></p>',
'<p>分类总数: <b>', $stats['cats'], '</b></p>',
'<p>标签总数: <b>', $stats['tags'], '</b></p>',
'<p>评论总数: <b>', $stats['comments'], '</b></p>',
'<p>用户总数: <b>', $stats['users'], '</b></p>',
'</div>';
}

然后,在你想要显示统计数据的地方插入这条函数(比如 sidebar.php 等文件):

<?php simple_stats(); ?>

这样也就 OK 了。数据显示的样式默认是不作修饰的,如果你想要外观好看一些,上面代码中有一个 class 类,给其中的 simple-stats 添加一些CSS代码美化一下就可以了,这方面这里就不提了。

  原文地址:http://www.sayblog.me/simple-statistics-on-wordpress.html

#wordpress##代码修改##统计#

免责声明:本文仅代表作者个人观点,与本网站无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
作者: chenmo 本文最后编辑于2013-2-26 10:04:41
chenmo

一个爱玩电脑,却又不沉迷游戏;喜欢网页设计,却又能力不足。喜欢网上到处灌,关注科技类新闻,寻搜有价值的网站。喜欢美剧,又喜爱电影,可以很宅,很宅,却又爱好户外骑行运动,喜欢接近大自然的心声。

作者的微博

评论:

2013-08-28 08:37
Hey users of this Web Site of cmsblog.cn.  I would like to introduce you to our new web shop, cheap replica brands direct from China.
Purchase goods direct from China http://clououtlet.biz/ - offering muitple products, also offering delivery services purchased anywhere in the world.  For retail customers clououtlet.biz - it is an opportunity to buy cheap goods from China, we have created a catalog of extensive directory of links to our stores on this page.  If you are interested in wholesale supply of Chinese products, the managers clououtlet.biz can assist with finding a suitable product, direct from the factory and ordered to your doorstep.

An important factory is that clououtlet.biz - it's not juse the site of goods from china, the company is headquarted in Guangzhou.  Wholesale clients have a unique opportunity to go directly to the producers, without the middle men involved.
Own office in china, this guarantees you the security of the transactions, and our ensures applied to borth wholesale and retail customers.

Sometimes, potential buyers do not know where or how to find a product that meets one or another specific needs.  In such situations, you can contact our managers and we shall find the product which you are looking for.

We will find you any pair of shoes of any brand!

On this page we will provide one of the samples of our products

[b]Shoes Christian Louboutin outlet pointed shoes 9cm patent black, price[/b] [color=darkgreen][b]$84.00[/b][/color].
[b]If you likes  Christian Louboutin outlet pointed shoes 9cm patent black, click the "I want to buy" below to find out where it is most advantageous to buy shoes brand Fake Christian Louboutin[/b]

[url=http://clououtlet.biz/christian-louboutin-high-heels/christian-louboutin-outlet-pointed-shoes-9cm-patent-black.html][img]http://clououtlet.biz/image/cache/data/img/CL072A_Christian Louboutin outlet pointed shoes 9cm patent black---1-500x500.jpg[/img][/url]
[url=http://clououtlet.biz/christian-louboutin-high-heels/christian-louboutin-outlet-pointed-shoes-9cm-patent-black.html][img]http://seopower.su/but.png[/img][/url]  

Category: [b]Christian Louboutin High Heels[/b]
Brand: [b]Imitation Christian Louboutin[/b]
Tags product: [b]Christian Louboutin outlet pointed shoes 9cm patent black, CL072, Christian Louboutin, Christian Louboutin High Heels, replica christian louboutin, christian louboutin replica, christian louboutin outlet, christian louboutin sale, christian louboutin shoes outlet, replica christian louboutin shoes, cheap louboutin outlet, christian louboutin fake, christian louboutin shoes sale, cheap christian louboutin shoes sale, cheap louboutin sale, christian louboutin knockoff, christian louboutin, louboutin shoes outlet, christian louboutin outlet online, louboutin, louboutin or, christianlouboutin, shoes christian louboutin, shoes by christian louboutin, shoes from christian louboutin, where can i get christian louboutin shoes, outlet louboutin, christian louboutin red shoes, red christian louboutin shoes, fake christian louboutins, christian louboutins fake, red louboutin[/b]

See more [url=http://clououtlet.biz/christian-louboutin-high-heels/christian-louboutin-outlet-pointed-shoes-9cm-patent-black.html]Replicas shoes[/url]

And here are the keywords for which people seek Imitation Christian Louboutin:
- buy [b]Replica Christian Louboutin[/b]
- order [b]Best replicas Christian Louboutin[/b]
- price [b]Replica Christian Louboutin[/b]
- online store [b]Best replicas Christian Louboutin[/b]
- [b]Cheap Replica Christian Louboutin[/b]
- [b]Cheap Replica Christian Louboutin[/b]
- [b]Replicas Christian Louboutin[/b] copy
- online store [b]Best replica Christian Louboutin[/b] UK
- online store [b]Best Imitation Christian Louboutin[/b] USA


Dear administration, please do not delete this topic.
For posting this topic, we are ready to pay the price, depending on the popularity of your website.
You can contact us on our website under "Contact Us"
Promo-code: #USRALIS88-DEBIL-VGHFOLOLO7765BFG4
2013-08-27 10:08
Hello webmasters of cmsblog.cn.  I would like to introduce you to our new online shop, cheap replica brands direct from China.
Purchase goods direct from China http://clououtlet.biz/ - offering muitple products, also offering delivery services purchased anywhere in the world.  For retail customers clououtlet.biz - it is an opportunity to buy cheap goods from China, we have created a catalog of extensive directory of links to our stores on this page.  If you are interested in wholesale supply of Chinese products, the managers clououtlet.biz can assist with finding a suitable product, direct from the factory and ordered to your doorstep.

An important factory is that clououtlet.biz - it's not juse the site of goods from china, the company is headquarted in Guangzhou.  Wholesale clients have a unique opportunity to go directly to the producers, without the middle men involved.
Own office in china, this guarantees you the security of the transactions, and our ensures applied to borth wholesale and retail customers.

Sometimes, potential buyers do not know where or how to find a product that meets one or another specific needs.  In such situations, you can contact us and we shall find the product which you are looking for.

We will find you any pair of shoes of any brand!

Below we will provide one of the samples of our products

[b]Shoes Christian Louboutin high heel shoe CL356 black, price[/b] [color=darkgreen][b]$93.00[/b][/color].
[b]If you  liked Christian Louboutin high heel shoe CL356 black, click the "I want to buy" below to find out where it is most advantageous to buy shoes brand Best Fake Christian Louboutin[/b]

[url=http://clououtlet.biz/christian-louboutin-peep-toe/christian-louboutin-high-heel-shoe-cl356-black.html][img]http://clououtlet.biz/image/cache/data/img/Christian Louboutin high heel shoe CL356 black-1-500x500.jpg[/img][/url]
[url=http://clououtlet.biz/christian-louboutin-peep-toe/christian-louboutin-high-heel-shoe-cl356-black.html][img]http://seopower.su/but.png[/img][/url]  

Category: [b]Christian Louboutin Peep Toe[/b]
Brand: [b]Best Imitation Christian Louboutin[/b]
Tags product: [b]Christian Louboutin high heel shoe CL356 black, CL356, Christian Louboutin, Christian Louboutin Peep Toe, replica christian louboutin, christian louboutin replica, christian louboutin outlet, christian louboutin sale, christian louboutin shoes outlet, replica christian louboutin shoes, cheap louboutin outlet, christian louboutin fake, christian louboutin shoes sale, cheap christian louboutin shoes sale, cheap louboutin sale, christian louboutin knockoff, christian louboutin, louboutin shoes outlet, christian louboutin outlet online, louboutin, louboutin or, christianlouboutin, shoes christian louboutin, shoes by christian louboutin, shoes from christian louboutin, where can i get christian louboutin shoes, outlet louboutin, christian louboutin red shoes, red christian louboutin shoes, fake christian louboutins, christian louboutins fake, red louboutin[/b]

See more [url=http://clououtlet.biz/christian-louboutin-peep-toe/christian-louboutin-high-heel-shoe-cl356-black.html]Imitation shoes[/url]

And here are the keywords for which people seek Best replicas Christian Louboutin:
- buy [b]Best Fake Christian Louboutin[/b]
- order [b]Replica Christian Louboutin[/b]
- price [b]Best replica Christian Louboutin[/b]
- online store [b]Cheap Replica Christian Louboutin[/b]
- [b]Replica Christian Louboutin[/b]
- [b]Imitation Christian Louboutin[/b]
- [b]Best Imitation Christian Louboutin[/b] copy
- online store [b]Best replica Christian Louboutin[/b] UK
- online store [b]Imitation Christian Louboutin[/b] USA


Dear administration, please do not delete this topic.
For posting this topic, we are ready to pay the price, depending on the popularity of your website.
You can contact us on our website under "Contact Us"
Promo-code: #USRALIS88-DEBIL-VGHFOLOLO7765BFG4

发表评论: