比如要在后台文章列表里显示文章点击量,将下面的代码添加到主题的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'); } }
转载声明:本站文章若无特别说明,皆为原创,转载请注明来源:www.88531.cn资享网,谢谢!^^
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END