您可以尝试使用自定义元字段来实现这一点。在里面functions.php
请填写以下内容:
// add the tracker in your header by using wp_footer hook
function is_viewed ($post_id) {
if ( !is_single() ) return;
/*
At times queries performed in other templates such as sidebar.php may corrupt certain conditional tags.
For instance, in header.php a conditional tag works properly but doesn\'t work in a theme\'s footer.php.
The trick is to put wp_reset_query before the conditional test in the footer.
https://codex.wordpress.org/Conditional_Tags#In_a_theme.27s_footer.php_file
*/
wp_reset_query();
if ( empty ( $post_id) ) {
global $post;
$post_id = $post->ID;
}
if ( !has_views($post_id) ){
// This post is visited for the first time
// add post meta to mark this post as viewed
add_post_meta($post_id,\'viewed\', "YES", true);
}
}
add_action( \'wp_footer\', \'is_viewed\');
function has_views($post_id){
if (!get_post_meta($post_id,\'viewed\',true)) return false;
else return true;
}
然后您可以使用
has_views($post_id)
您想测试帖子是否第一次访问的任何地方