在wordpress上显示帖子的查看次数

在wordpress上显示帖子的查看次数,wordpress,wordpress-theming,Wordpress,Wordpress Theming,我正在开发一个wordpress,使用wordpress流行帖子插件,并面临这个问题 如何在博客的分类列表页面或索引页面上显示访客浏览页面的次数。这个信息已经存在,插件正在使用它在边栏中显示相同的信息,不知道如何使用相同的插件数据来显示博客页面上的页面浏览量 我试图找到这个,但没有得到我想要的 请告诉我怎么做 我使用的插件是插件提供了一个名为wpp\u get\u views的函数。 如果您知道要显示其视图计数的帖子/页面的ID,则只需使用该ID作为参数调用函数即可。例如: $count = w

我正在开发一个wordpress,使用wordpress流行帖子插件,并面临这个问题

如何在博客的分类列表页面或索引页面上显示访客浏览页面的次数。这个信息已经存在,插件正在使用它在边栏中显示相同的信息,不知道如何使用相同的插件数据来显示博客页面上的页面浏览量

我试图找到这个,但没有得到我想要的

请告诉我怎么做


我使用的插件是插件提供了一个名为
wpp\u get\u views
的函数。 如果您知道要显示其视图计数的帖子/页面的ID,则只需使用该ID作为参数调用函数即可。例如:

$count = wpp_get_views($post->ID); 

无需插件即可轻松完成。

要计算帖子浏览量,首先要做的是将以下代码添加到WordPress主题functions.php

<?php
/*
 * Set post views count using post meta//functions.php
 */
function customSetPostViews($postID) {
    $countKey = 'post_views_count';
    $count = get_post_meta($postID, $countKey, true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID, $countKey);
        add_post_meta($postID, $countKey, '1');
    }else{
        $count++;
        update_post_meta($postID, $countKey, $count);
    }
}
?>

现在我们将在single.php中调用此函数来更新数据库中的计数值

<?php 
    customSetPostViews(get_the_ID());//single.php
?>

现在,在相同的single.php文件中,如果我们想显示后期视图计数,我们可以使用以下代码:

<?php
    $post_views_count = get_post_meta( get_the_ID(), 'post_views_count', true );
    // Check if the custom field has a value.
    if ( ! empty( $post_views_count ) ) {
        echo $post_views_count;
    }
?>
<?php//popular post query
    query_posts('meta_key=post_views_count&posts_per_page=5&orderby=meta_value_num&
    order=DESC');
    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();
?>

现在,按帖子查看次数降序显示所有流行帖子。使用此代码:

<?php
    $post_views_count = get_post_meta( get_the_ID(), 'post_views_count', true );
    // Check if the custom field has a value.
    if ( ! empty( $post_views_count ) ) {
        echo $post_views_count;
    }
?>
<?php//popular post query
    query_posts('meta_key=post_views_count&posts_per_page=5&orderby=meta_value_num&
    order=DESC');
    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();
?>

  • 快乐编码