Last active
June 1, 2020 16:02
-
-
Save Fobiya/8e6e5fa4fb8ceb02f1e00e2e8f0c5b71 to your computer and use it in GitHub Desktop.
WP Views post_views_count
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Добавим этот код 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 "0 просмотров"; | |
} | |
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); | |
} | |
} | |
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 просмотров"; | |
} | |
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); | |
} | |
} | |
Здесь представлено 2 функции: | |
setPostViews() — сбор статистики. | |
getPostViews () — вывод. | |
Далее необходимо добавить код: | |
<?php setPostViews(get_the_ID()); ?> | |
в файлы где вы хотите считывать просмотры.Например, single.php ( в моем случае content-single.php). | |
Последнее, что нужно сделать — вставить код: | |
<?php echo getPostViews(get_the_ID()); ?> | |
в место где буде выводиться число просмотров (возле даты публикации, автора, количества комментариев и т.п.). | |
Мы разобрали два способа вывода счетчика просмотров записей, какой из них выбрать решать вам. | |
Помимо этого вы можете смотреть количество просмотров прямо в админке вашего сайта. Для этого нужно в functions.php добавить следующее: | |
add_filter('manage_posts_columns', 'posts_column_views'); | |
add_action('manage_posts_custom_column', 'posts_custom_column_views',5,2); | |
function posts_column_views($defaults){ | |
$defaults['post_views'] = __('просмотров'); | |
return $defaults; | |
} | |
function posts_custom_column_views($column_name, $id){ | |
if($column_name === 'post_views'){ | |
echo getPostViews(get_the_ID()); | |
} | |
} | |
В результате появится дополнительная колонка, с количеством просмотров вашего поста. | |
Все работает и проверено на этом блоге. | |
-------------------------------------------------------------------------------------------------------------------------------- | |
/**** getPostViews ****/ | |
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 views"; | |
} | |
return $count.' views'; | |
} | |
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); | |
} | |
} | |
/**** getPostViews ****/ | |
IN SINGEL | |
<?php setPostViews(get_the_ID()); ?> щиталка | |
<?php echo getPostViews($id); ?> | |
/// popular post ------> | |
<?php | |
$id__page = get_the_ID(); | |
// The Query | |
$new_query = new WP_Query(array( 'post_type' => 'blog','posts_per_page' => 30, 'meta_key' => 'post_views_count', 'orderby' => 'post_views_count', 'order' => 'DESC' , 'post__not_in' => array($id__page))); | |
// The Loop | |
if ($new_query->have_posts()) { | |
echo '<ul class="left_blog">'; | |
while ($new_query->have_posts()) { | |
$new_query->the_post(); | |
$categories = get_the_category(); | |
if (!empty($categories)) { | |
$caty = esc_html($categories[0]->name); | |
} | |
echo ' | |
<li> | |
<span class="views">' . getPostViews($id) . '</span> | |
<div class="top"><a data-attr="' . $caty . '">' . $caty . '</a> | <span class="data">' . get_the_date('Y-m-d h:i') . 'UTC</span></div> | |
<h3><a href="' . get_permalink() . '" target="_blank">' . get_the_title() . '</a></h3> | |
</li>'; | |
} | |
echo '</ul>'; | |
} else { | |
// no posts found | |
} | |
/* Restore original Post Data */ | |
wp_reset_postdata(); | |
?> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment