定制服务
承接WordPress等系统建站仿站、二次开发、主题插件定制等
在线咨询
  • 请直接说明需求,勿问在否

联系我们
wordpress
wordpress收费下载资源主题

wordpress 在文章管理列表添加自定义列

比如要在后台文章列表里显示文章点击量,将下面的代码添加到主题的functions.php中即可:

add_action('manage_posts_custom_column', 'add_postviews_column_content');
 add_filter('manage_posts_columns', 'add_postviews_column');
 add_action('manage_pages_custom_column', 'add_postviews_column_content');
 add_filter('manage_pages_columns', 'add_postviews_column');
 function add_postviews_column($defaults) {
 $defaults['views'] = '点击量';
 return $defaults;
 }

function add_postviews_column_content($column_name) {
 if($column_name == 'views') {
 if(function_exists('MBT_views')) { MBT_views(''); } //函数MBT_views获取点击量
 }
 }

//以下代码是获取点击量,请按需添加
if ( ! function_exists( 'MBT_views' ) ) :
 function Mobantu_record_visitors(){
 if (is_singular()) 
 {
 global $post;
 $post_ID = $post->ID;
 if($post_ID) 
 {
 $post_views = (int)get_post_meta($post_ID, 'views', true);
 if(!update_post_meta($post_ID, 'views', ($post_views+1))) 
 {
 add_post_meta($post_ID, 'views', 1, true);
 }
 }
 }
 }
 add_action('wp_head', 'Mobantu_record_visitors'); 
 
 function MBT_views($after=''){
 global $post;
 $post_ID = $post->ID;
 $views = (int)get_post_meta($post_ID, 'views', true);
 echo $views, $after;
 }
 endif;

顺便加上排序

add_filter('manage_edit-post_sortable_columns', 'sort_postviews_column');
 add_filter('manage_edit-page_sortable_columns', 'sort_postviews_column');
 function sort_postviews_column($defaults)
 {
 $defaults['views'] = 'views';
 return $defaults;
 }
 add_action('pre_get_posts', 'sort_postviews');
 function sort_postviews($query) {
 if(!is_admin())
 return;
 $orderby = $query->get('orderby');
 if('views' == $orderby) {
 $query->set('meta_key', 'views');
 $query->set('orderby', 'meta_value_num');
 }
 }

Modia 自媒体 wordpress主题

0 条评论

模板兔8月新品Modia WordPress自媒体资讯主题限时8折优惠,立即抢购>>