Я использую тему WordPress RealHomes, и мне нужно подсчитать количество кликов по каждому свойству. Я хочу, чтобы счетчик сбрасывался после каждого сеанса, поэтому мне нужны куки, но я не знаю, как их реализовать. Мне уже удалось написать счетчик для кликов, но они не сбрасываются, они просто продолжают считать.
Это мои функции для получения и установки счетчика, getPostViews может вызываться там, где вы хотите отобразить счетчик, и setPostViews вызывается в начале файла свойств single.php.
function getPostViews($postID){
$count_key = 'post_views_count';
$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 View";
}
return $count;
}
function setPostViews($postID) {
$count_key = 'post_views_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);
}
add_action('end_session_action','end_session');
}
Я нашел код для реализации нового cookie в php, но я не знаю, как реализовать его для моего примера.
function viewed_cookie ($postID) {
// Time of user's visit
$visit_time = date('F j, Y g:i a');
// Check if cookie is already set
if(isset($_COOKIE['visited_cookie'])) {
// Do this if cookie is set
function postViewed() {
// Use information stored in the cookie
$lastvisit = $_COOKIE['visited_cookie'];
$string .= $lastvisit;
// Delete the old cookie so that we can set it again with updated info
unset($_COOKIE['visited_cookie']);
return $string;
}
} else {
// Do this if the cookie doesn't exist
function postViewed() {
$string .= 'New here? Check out these resources...' ;
return $string;
}
}
add_shortcode('viewed', 'postViewed');
// Set or Reset the cookie
setcookie('visited_cookie', $visit_time);
}
add_action('init', 'viewed_cookie');