Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在wordpress中显示查看计数帖子?_Php_Wordpress - Fatal编程技术网

Php 在wordpress中显示查看计数帖子?

Php 在wordpress中显示查看计数帖子?,php,wordpress,Php,Wordpress,我正在使用此代码显示wordpress中的热门帖子,虽然效果不错,但我无法显示浏览量,请帮助:) 这是一页 wpb_set_post_views(get_the_ID()); 这是一篇查看帖子的文章 <?php $popularpost = new WP_Query( array( 'posts_per_page' => 4, 'meta_key' => 'wpb_post_views_count', 'orderby' => 'meta_value_num', 'o

我正在使用此代码显示wordpress中的热门帖子,虽然效果不错,但我无法显示浏览量,请帮助:)

这是一页

wpb_set_post_views(get_the_ID());
这是一篇查看帖子的文章

<?php 
$popularpost = new WP_Query( array( 'posts_per_page' => 4, 'meta_key' => 'wpb_post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC'  ) );
while ( $popularpost->have_posts() ) : $popularpost->the_post();

the_title();

endwhile;

?>

要显示页面计数,您应该通过
get\u post\u meta()
使用meta键获取值。在循环中,您可以使用
全局$post
获取当前的post ID

$popularpost = new WP_Query( array( 'posts_per_page' => 4, 'meta_key' => 'wpb_post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC'  ) );
while ( $popularpost->have_posts() ) : $popularpost->the_post();
    // print the post title
    the_title();
    // get the count using the meta key wpb_post_views_count
    $count = get_post_meta( $post->ID, 'wpb_post_views_count', true );
    // echo the current count
    echo $count;
endwhile;
您还可以简化记录页面浏览量的功能。您可以使用
update\u post\u meta()
函数,而不是先删除然后添加,如果该值不存在,则该函数将添加该值,如果存在,则更新该值。通过对照
false
检查
get\u post\u meta()
的结果,我们可以确定是否需要将计数初始化为0。一旦我们有了一个值,将其更新为当前值+1。请注意,在高流量站点上,由于竞争条件同时更新多个请求的值,因此这不能保证准确

function wpb_set_post_views( $post_id ) {
    // if $count is exactly false, set it to 0, otherwise use the value from the db
    if ( false === ( $count = get_post_meta( $post_id, 'wpb_post_views_count', true ) ) ){
        $count = 0;
    }
    // update the value +1 pageview
    update_post_meta( $post_id, 'wpb_post_views_count', $count+1 );
}

要计算帖子浏览量,首先要做的是将以下代码添加到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();
?>

  • 快乐编码