WordPress教程

WordPress后台文章列表添加浏览点赞数

阿里云

之前文章大家分享过怎样在wordpress 后台列表添加文章阅读数量,今天分享一个加强版本,同时在 WordPress 模板列表后台显示阅读与点赞的数量,通过列表就直观的可以看到当前文章的阅读情况及点赞情况,更好方便站长对文章内容方向做统计。比如我想查看文章的浏览量和点赞数,那就加两栏。实现的方式是通用的,假如你想加其他的数据,增加对应的字段就可以了。

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

  1. //~ 数字 格式化
  2. function num2tring($num) {
  3.     if ($num >= 10000) {
  4.         $num = round($num / 10000 * 100) / 100 .' w'; // 以万为单位
  5.     } elseif($num >= 1000) {
  6.         $num = round($num / 1000 * 100) / 100 . ' k'; // 以千为单位
  7.     } else {
  8.         $num = $num;
  9.     }
  10.     return $num;
  11. }
  12.  
  13. //~ 在后台文章列表增加2列数据,展示浏览量和点赞数
  14. add_filter( 'manage_posts_columns', 'hbao_customer_posts_columns' );
  15. function hbao_customer_posts_columns( $columns ) {
  16.   $columns['views'] = '浏览量';
  17.   $columns['likes'] = '点赞数';
  18.   return $columns;
  19. }
  20.  
  21. //~ 输出浏览量和点赞数
  22. add_action('manage_posts_custom_column', 'hbao_customer_columns_value', 10, 2);
  23. function hbao_customer_columns_value($column, $post_id){
  24.        if($column=='views'){
  25. 	  $count = num2tring(get_post_meta($post_id,'post_views_count',true)); // 注意 post_views_count是字段名,根据你自己的来
  26. 	  if(!$count){
  27.             $count = 0;
  28. 	  }
  29. 	  echo $count;
  30. 	}
  31. 	if($column=='likes'){
  32. 	  $likes_count = get_post_meta($post_id,'bigfa_ding',true); // 注意 bigfa_ding是字段名,根据你自己的来
  33. 	  if(!$likes_count) {
  34.             $likes_count = 0;
  35. 	  }
  36. 	  echo $likes_count;
  37. 	}
  38. 	return;
  39. }
也想出现在这里?联系我们
创客主机

将上面代码复制到 functions.php 中即可。

扩展阅读

  1. //get_post_meta函数
  2. get_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, 则返回一个数组字符串型。

WordPress 后台文章列表添加浏览点赞数

已有 374 人购买
查看演示升级 VIP立刻购买

收藏
(2)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!