您可以使用这些功能跟踪帖子视图。创建新文件并将其调用到functions.php
收集足够的数据后,可以为仪表板创建一个元框,并可以使用图表模块查看每月/每年的帖子视图等。
add_action( \'wp_head\', \'get_post_id\' );
function get_post_id() {
$postID = get_the_ID();
return $postID;
}
function getPostViews() {
$postID = get_post_id();
$count_key = \'views\';
$count = get_post_meta( $postID, $count_key, true );
if ( $count == \'\' ) {
delete_post_meta( $postID, $count_key );
add_post_meta( $postID, $count_key, \'0\' );
return __( \'0\', \'sagive\' );
}
return number_format( $count, \'0\', \',\', \'.\' ) . __( \'\', \'sagive\' );
}
remove_action( \'wp_head\', \'adjacent_posts_rel_link_wp_head\', 10, 0 );
add_action( \'wp_head\', \'setPostViews\' );
function setPostViews() {
if ( ! is_home() && ! is_robots() && ! is_user_logged_in() ) {
$postID = get_post_id();
$count_key = \'views\';
$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 );
}
}
}