您可能需要对此进行一点修改,以确保将内容放置在您想要的位置,但我写了一篇文章,它可以计算视图posts
并显示管理员的视图计数:
//ADD TRACKER FUNCTION TO ALL SINGLE VIEWS
function custom_hook_tracker( $post_id ) {
if( !is_single() ) return;
if( empty( $post_id ) ) {
global $post;
$post_id = $post->ID;
}
custom_track_post_views( $post_id );
}
add_action( \'wp_head\', \'custom_hook_tracker\' );
//ADD VIEW TRACKER
function custom_track_post_views( $postID ) {
$count_key = \'custom_view_count\';
$count = get_post_meta( $postID, $count_key, true );
if( $count==\'\' ) {
$count = 0;
delete_post_meta( $postID, $count_key );
add_post_meta( $postID, $count_key, \'0\' );
} else {
$count++;
update_post_meta( $postID, $count_key, $count );
}
//REMOVE THIS LINE IF YOU USE THE TEMPLATE TAG
if( is_single() && current_user_can( \'administrator\' ) ) { //remove or adjust the \'&& current_user_can( \'administrator\' )\' to modify who CAN see the counts
//ALSO REMOVE THIS LINE IF YOU USE THE TEMPLATE TAG
echo \'<div style="position:absolute;top:2.75rem;width:auto;display:block;float:left;background:#FFF;border-radius:4px;box-shadow:#333 1px 1px 4px;padding:0.25rem 0.5rem;margin-left:-5px;color:#777;"><small>Views: \'.$count.\'</small></div>\';
}
}
//REMOVE PRE-FETCHING TO KEEP COUNTS ACCURATE
remove_action( \'wp_head\', \'adjacent_posts_rel_link_wp_head\', 10, 0);
好吧,我不能就这样离开它-这是模板标签:
function custom_view_counts() {
$count_key = \'custom_view_count\';
$count = get_post_meta( $postID, $count_key, true );
echo \'<div class="view-tracker"><small>Views: \'.$count.\'</small></div>\';
}
然后,要显示它,请将其精确地放置在主题文件中您希望它显示的位置:
<?php custom_view_counts() ;?>
您需要编写css来处理/设置样式/针对.view-tracker
类别/部门。
您基本上可以构建自己的插件,或者将以上所有内容都放到您的函数中。php。