admin管理员组文章数量:1336215
I am trying to display post view counts.
I've tried below function to display the post view count, but I'm getting same post view for all the post in loop. Not getting correct post view count.
Function:
<?php
// function to display number of posts.
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 to 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);
}
}
// Add it to a column in WP-Admin
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'] = __('Views');
return $defaults;
}
function posts_custom_column_views($column_name, $id){
if($column_name === 'post_views'){
echo getPostViews(get_the_ID());
}
}
?>
Loop Code:
<ul>
<?php $the_query = new WP_Query( 'showposts=5' ); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
<?php echo getPostViews(get_the_ID()); ?> //post View COde
<?php echo substr(strip_tags($post->post_content), 0, 250);?></li>
<?php endwhile;?>
</ul>
Need suggestions to display Correct Post View Count in while Loop
I am trying to display post view counts.
I've tried below function to display the post view count, but I'm getting same post view for all the post in loop. Not getting correct post view count.
Function:
<?php
// function to display number of posts.
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 to 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);
}
}
// Add it to a column in WP-Admin
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'] = __('Views');
return $defaults;
}
function posts_custom_column_views($column_name, $id){
if($column_name === 'post_views'){
echo getPostViews(get_the_ID());
}
}
?>
Loop Code:
<ul>
<?php $the_query = new WP_Query( 'showposts=5' ); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
<?php echo getPostViews(get_the_ID()); ?> //post View COde
<?php echo substr(strip_tags($post->post_content), 0, 250);?></li>
<?php endwhile;?>
</ul>
Need suggestions to display Correct Post View Count in while Loop
Share Improve this question edited May 14, 2014 at 5:01 Pieter Goosen 55.4k23 gold badges115 silver badges210 bronze badges asked May 14, 2014 at 4:39 Jack TorrisJack Torris 2513 gold badges6 silver badges15 bronze badges 2 |3 Answers
Reset to default 6Add this to single.php, and make sure you paste in inside the loop.
<?php setPostViews(get_the_ID()); ?>
Lastly, to display the number of views a post has, just add this where you want it to be displayed:
<?php echo getPostViews(get_the_ID()); ?>
.
<?php
// function to display number of posts.
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 to 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);
}
}
?>
If you are storing the meta data correctly, then you can use the following code to display the count
echo (int) get_post_meta(get_the_ID(), 'post_views_count', true) . ' View(s)';
For the value 0 in add_post_meta
the view count is not updating. Try the line add_post_meta($postID, $count_key, '1');
instead of line add_post_meta($postID, $count_key, '0');
.
本文标签: functionsHow to Display Post View Count
版权声明:本文标题:functions - How to Display Post View Count 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742400422a2467777.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
getPostViews
function? – Chittaranjan Commented May 14, 2014 at 7:12