笔记此代码未经测试!
<?php
function my_visitor_cookie($post_id) {
if ( empty($post_id) ) {
global $post;
$post_id = $post->ID;
}
// get post meta
$count = get_post_meta($post_id, \'unique_post_visits\', true);
// if there was no meta value
if( empty($count) ) {
$count = 0;
}
// check if cookie was already set (cookie name for the current post)
if( !isset($_COOKIE[\'my_visitor_\' . $post_id]) ) {
// set visitor cookie if it is not set already
setcookie(
\'my_visitor_\' . $post_id, // cookie name for the current post
$post_id, // any value, shot in the dark
DAY_IN_SECONDS // WordPress time constant
);
// increase count
$count++;
// update count
update_post_meta($post_id, \'unique_post_visits\', $count);
}
}
// add_action(\'wp_head\', \'my_visitor_cookie\');
add_action(\'init\', \'my_visitor_cookie\'); // updated according to the comments