给wordpress后台列表添加文章点赞数


之前文章大挖和大家分享过怎样在wordpress后台列表添加文章阅读数量,今天分享一个升级版,同时在wordpress模板列表后台显示阅读与点赞的数量,通过列表就直观的可以看到当前文章的阅读情况及点赞情况,更好方便站长对文章内容方向做统计。

其实添加阅读与添加点赞的原理是相通的,假如你还想加其他的数据,增加对应的字段就可以了。

将以下代码复制到functions.php中即可。

123456789101112131415161718192021222324252627282930313233343536373839//~ 数字 格式化function num2tring($num) {if ($num >= 10000) {$num = round($num / 10000 * 100) / 100 .' w'; // 以万为单位} elseif($num >= 1000) {$num = round($num / 1000 * 100) / 100 . ' k'; // 以千为单位} else {$num = $num;}return $num;} //~ 在后台文章列表增加2列数据,展示浏览量和点赞数add_filter( 'manage_posts_columns', 'hbao_customer_posts_columns' );function hbao_customer_posts_columns( $columns ) {$columns['views'] = '浏览量';$columns['likes'] = '点赞数';return $columns;} //~ 输出浏览量和点赞数add_action('manage_posts_custom_column', 'hbao_customer_columns_value', 10, 2);function hbao_customer_columns_value($column, $post_id){ if($column=='views'){$count = num2tring(get_post_meta($post_id,'post_views_count',true)); // 注意 post_views_count是字段名,根据你自己的来if(!$count){$count = 0;}echo $count;}if($column=='likes'){$likes_count = get_post_meta($post_id,'bigfa_ding',true); // 注意 bigfa_ding是字段名,根据你自己的来if(!$likes_count) {$likes_count = 0;}echo $likes_count;}return;}

扩展阅读,get_post_meta函数

1get_post_meta( int $post_id, string $key = '', bool $single = false )

使用方法:

1<?php $meta_values = get_post_meta($post_id, $key, $single); ?>

参数:
$post_id
(int) (Required) Post ID.
(整数) (必须的)包含此meta文章的ID

$key
(string) (Optional) The meta key to retrieve. By default, returns data for all keys.
(字符串) (必须的) 此meta的名称

$single
(bool) (Optional) Whether to return a single value.
(布尔型) (可选) 如果设为true, 则返回单个的meta的值。如果设为false, 则返回一个数组字符串型。

参考资料:http://developer.wordpress.org/reference/functions/get_post_meta/


上一篇:解决阿里云盾wordpress

下一篇:刷新不影响文章浏览量的wordpress点击量统计代码


Copyright © 2002-2019 测速网 www.inhv.cn 皖ICP备2023010105号
测速城市 测速地区 测速街道 网速测试城市 网速测试地区 网速测试街道
温馨提示:部分文章图片数据来源与网络,仅供参考!版权归原作者所有,如有侵权请联系删除!

热门搜索 城市网站建设 地区网站制作 街道网页设计 大写数字 热点城市 热点地区 热点街道 热点时间 房贷计算器