Счетчик просмотра страниц
Создаем счетчик просмотра страниц сайта. Для того чтобы фиксировать и выводить статистику просмотра страницы, нужно поместить код в functions.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 "нет";
}
echo _e('');
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);
}
}
Добавить код в шаблон записи и если необходимо страницы
<?php setPostViews(get_the_ID()); ?>
Выводим количество там где надо
<?php echo getPostViews(get_the_ID()); ?>
Вывести самые «просматриваемые» записи
<?php
query_posts('meta_key=post_views_count&orderby=meta_value_num&order=DESC');
if (have_posts()) : while (have_posts()) : the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php
endwhile; endif;
wp_reset_query();
?>
Плагина Wp-PostViews
Есть альтернатива в виде плагина Wp-PostViews. В нем функционал более расширен. Например: выводить записи с наибольшим количеством просмотров (Самое популярное).
Сортировка по количеству просмотров
Можно создать функционал сортировки записей по количеству просмотров.
Популярные записи
Самые просматриваемые (популярные) записи можно отмечать лэблом (например HIT).
<?php $views_count = get_post_meta($post->ID, 'post_views_count', true); if ($views_count > 1000) : ?><div>HIT!</div><?php endif; ?>
Либо в строку формирования класса записи
<div class="<?php $allClasses = get_post_class(); foreach ($allClasses as $class) { echo $class . " "; } ?> <?php echo $new_class; ?> <?php $views_count = get_post_meta($post->ID, 'post_views_count', true); if ($views_count > 1000) : ?>hit<?php endif; ?>" id="post-<?php the_ID(); ?>">
[site-socialshare]
