如何显示后期查看计数

时间:2014-05-14 作者:Jack Torris

我正在尝试显示后期视图计数。

我已经尝试了下面的函数来显示后期视图计数,但我得到的所有循环中的后期视图都是相同的。无法获取正确的后期视图计数。

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>
需要建议在while循环中显示正确的后期视图计数

3 个回复
SO网友:Abdo-Host

将此添加到single。php,并确保粘贴到循环内部。

<?php setPostViews(get_the_ID()); ?>
最后,要显示帖子的视图数,只需将其添加到要显示的位置:

<?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);
    }
}

?>

SO网友:Chittaranjan

如果正确存储元数据,则可以使用以下代码显示计数

echo (int) get_post_meta(get_the_ID(), \'post_views_count\', true) . \' View(s)\';

SO网友:user169025

对于中的值0add_post_meta 视图计数未更新。试试这条线add_post_meta($postID, $count_key, \'1\'); 而不是线条add_post_meta($postID, $count_key, \'0\');.

结束

相关推荐

Posts for next month

如何将设置为“未来”的帖子显示在循环中?我正试图找出如何在未来几个月内获得帖子。因此,如果我们在四月,我想显示五月的帖子。可以将其视为即将发布的帖子。我一直在看http://codex.wordpress.org/Function_Reference/WP_Query#Date_Parameters 我也一直在看这个:http://joshpress.net/blog/using-new-date-queries-wordpress-3-7/下面是下个月的帖子:<?php //get an