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
  • 1 Whats the source of the code? – Brad Dalton Commented May 14, 2014 at 4:44
  • Can you please explain why you are deleting and then adding view count in getPostViews function? – Chittaranjan Commented May 14, 2014 at 7:12
Add a comment  | 

3 Answers 3

Reset to default 6

Add 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