Wordpress实现php代码版文章浏览数
本文章下面来给各位同学介绍一下Wordpress实现php代码版文章浏览数,这里就可以浏览到每篇文章的具体浏览资料了,如果需要更详细的我们可使用WP-Postviews 这款插件来统计文章浏览数了.
我个人只要[统计和输出浏览数]功能,所以挪用了主体函数部分,代码如下:
function custom_the_views( $post_id , $echo =true, $unit = ' views' ) { $count_key = 'views' ; $views = get_post_custom( $post_id ); $views = intval ( $views [ 'views' ][0]); if ( $views == '' ) { return '' ; } else { if ( $echo ) { echo number_format_i18n( $views ) . $unit ; } else { return number_format_i18n( $views ) . $unit ; } } } function set_post_views() { global $post ; $post_id = intval ( $post ->ID); $count_key = 'views' ; $views = get_post_custom( $post_id ); $views = intval ( $views [ 'views' ][0]); if (is_single() || is_page()) { if (!update_post_meta( $post_id , 'views' , ( $views + 1))) { add_post_meta( $post_id , 'views' , 1, true); } } } add_action( 'get_header' , 'set_post_views' );参数说明:
1. $post_id: 文章ID,一般填 $post->ID
2. $echo: true/false,显示/获取浏览数(默认是显示-true)
3. $views: 浏览数单位(默认是:空格+views)
一般调用方法, 代码如下:
<?php if ( function_exists('custom_the_views') ) custom_the_views($post->ID);?>
提示 :如果原来用了 WP-Postviews 这个插件,只要把如下代码:
<?php the_views(); ?> 改为 <?php custom_the_views($post->ID); ?>即可。
或者是如下代码:
<?php if ( function_exists('the_views') ) the_views(); ?>
改为:<?php if ( function_exists('custom_the_views') ) custom_the_views($post->ID);
查看更多关于Wordpress实现php代码版文章浏览数 - WordPress的详细内容...