Как вывести популярные записи на WordPress в сайдбаре или в подвале статьи без плагина.
Вставляем в конец файла 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);
}
}
Для сбора информации о просмотрах отдельной страницы редактируем файл вывода записи single.php
:
<!-- для сбора информации о
просмотрах страницы, вставить в самый конец файла -->
<?php setPostViews(get_the_ID()); ?>
<!-- / для сбора информации о
просмотрах страницы-->
Далее в нужное место в файле single.php
вставляем следующий код:
- для вывода записей в виде ссылок
<!-- Популярные
записи в виде ссылок-->
<div class="pohoji-tems">
<h2>Популярные статьи:</h2>
<ul>
<?php
$args
= array( 'posts_per_page'
=> 5, 'meta_key'
=> 'post_views_count', 'orderby'
=> 'meta_value_num', 'order'
=> 'DESC'
);
query_posts($args);
while
( have_posts() ) : the_post();
?>
<li>
<a onclick="return !window.open(this.href)"
<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
</li>
<?php endwhile; wp_reset_query(); ?>
</ul>
</div>
- для вывода записей с миниатюрами
<!-- Популярные записи с миниатюрами -->
<div class="pohoji-tems">
<h2>Популярные статьи:</h2>
<ul>
<?php
$args
= array( 'posts_per_page'
=> 5, 'meta_key'
=> 'post_views_count', 'orderby'
=> 'meta_value_num', 'order'
=> 'DESC'
);
query_posts($args);
while
( have_posts() ) : the_post();
?>
<li>
<a onclick="return !window.open(this.href)"
href="<?php the_permalink() ?>">
<?php the_post_thumbnail('thumbnail'); ?></a>
<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
</li>
<?php endwhile; wp_reset_query(); ?>
</ul>
</div>
0 комментариев