نمایش پربازدید ترین مطالب در وردپرس
دوستان زیادی نیاز به کدی داشتن که هم تعداد بازدید یک موضوع رو نشون بده و هم اینکه پربازدیدترین ها رو نشون بده.
طبق توضیحات عمل کنید و لذت ببرید.
کد در فایل فانکشن:
[codesyntax lang=”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.' 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);
}
}
[/codesyntax]
کد در حلقه پست:
[codesyntax lang=”php”]
<?php setpostviews(get_the_ID()); ?>
[/codesyntax]
نمایش تعداد بازدید هر پست:
[codesyntax lang=”php”]
<?php echo getpostviews(get_the_ID()); ?>
[/codesyntax]
نمایش پربازدیدترین ها:
[codesyntax lang=”php”]
<?php
query_posts('meta_key=post_views_count&orderby=meta_value_num&order=DESC&showposts=10');
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();
?>
[/codesyntax]