【问题标题】:Why the post view count function not working?为什么帖子查看计数功能不起作用?
【发布时间】:2019-02-22 05:06:15
【问题描述】:

美好的一天。我一直在研究如何根据this 链接跟踪帖子浏览次数

但是,当我调用存储在循环中的 single.php 中的函数时,无论我刷新浏览器多少次,我刚刚发布的帖子仍然保持 0 视图(帖子没有更新帖子视图计数)。如果有任何帮助,我将不胜感激。

我的 functions.php 文件 - 帖子浏览次数的代码

function subh_set_post_view($postID)
{
    $count_key = 'post_views_count';
    $count = (int) get_post_meta($postID, $count_key, true);
    if ($count == '') {
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        return $count . 'View ';
    } else {
        $count++;
        update_post_meta($postID, $count_key, (string) $count);
    }
}

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';
}

/**
 * Add a new column in the admin panel posts list
 *
 * @param $defaults
 *
 * @return mixed
 */
function subh_posts_column_views($defaults)
{
    $defaults['post_views'] = __('Views');

    return $defaults;
}

/**
 * Display the number of views for each posts on the admin panel
 *
 * @param $column_name
 * @param $id
 *
 * @return void simply echo out the number of views
 */
function subh_posts_custom_column_views($column_name, $id)
{
    if ($column_name === 'post_views') {
        echo subh_get_post_view(get_the_ID());
    }
}

add_filter('manage_posts_columns', 'subh_posts_column_views');
add_action('manage_posts_custom_column', 'subh_posts_custom_column_views', 5, 2);

我的 single.php 文件:

<?php 
    get_header();
?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" />
<div id="breadcrumbs" class="stuck_position">
    <div class="container">
        <div class="row">
            <div class="moduletable   col-sm-12">
                <div class="module_container">
                    <ul class="breadcrumb">
                        <li>
                            <a href="<?php echo esc_url(home_url()); ?>" class="pathway">
                                Home
                            </a>
                            <span class="divider">&nbsp;/&nbsp;</span>
                        </li>

                        <li class="active">
                            <span>
                                <?php the_title();?>
                            </span>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
</div>

<div id="content">
    <div class="container">
        <div class="row">
            <div class="content-inner">
                <!-- Left sidebar -->

                <div id="component" class="col-sm-12">
                    <main role="main">
                        <div id="system-message-container">
                        </div>

                        <article class="page-item">
                            <header class="item_header">
                                <h3 class="item_title">
                                    <span class="item_title_part_first">
                                        <?php the_title();?>
                                    </span>
                                </h3>
                            </header>
                            <div class="item_info">
                                <dl class="item_info_dl">
                                    <dd>
                                        <address class="item_createdby">
                                            <?php echo get_the_author(); ?> </address>
                                    </dd>
                                </dl>
                            </div>
                            </div>
                        </article>
                    </main>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</div>
<?php 
    if(function_exists('subh_set_post_view')) { 
    subh_set_post_view(get_the_ID()); 
    }
        echo getPostViews(get_the_ID());

?>
<?php endwhile; ?>
<?php endif; ?>  
<?php get_footer();?>

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    试试这个代码。

    function subh_set_post_view($postID)
    {
    
        $count_key = 'post_views_count';
        $count = (int) get_post_meta($postID, $count_key, true);
    
        if ($count == 0) {      
            $count++;
            delete_post_meta($postID, $count_key);
            add_post_meta($postID, $count_key, $count++);
        } else {        
            $count++;
            update_post_meta($postID, $count_key, $count);
        }
    
    }
    
    function getPostViews($postID){
        $count_key = 'post_views_count';
        $count = get_post_meta($postID, $count_key, true);    
        return $count.' Views';
    }
    
    /**
     * Add a new column in the admin panel posts list
     *
     * @param $defaults
     *
     * @return mixed
     */
    function subh_posts_column_views($defaults)
    {
        $defaults['post_views'] = __('Views');
    
        return $defaults;
    }
    
    /**
     * Display the number of views for each posts on the admin panel
     *
     * @param $column_name
     * @param $id
     *
     * @return void simply echo out the number of views
     */
    function subh_posts_custom_column_views($column_name, $id)
    {
        if ($column_name === 'post_views') {
            echo getPostViews(get_the_ID());
        }
    }
    
    add_filter('manage_posts_columns', 'subh_posts_column_views');
    add_action('manage_posts_custom_column', 'subh_posts_custom_column_views', 5, 2);
    

    【讨论】:

    • 很高兴为您提供帮助
    【解决方案2】:

    get_the_ID() : 检索 WordPress 中当前项目的 ID 循环。

    这就是你的功能不起作用的原因。 get_the_ID() 只能在循环内使用,不能在 single.php 上使用

    阅读Getting the WordPress Post ID of current post的所有答案和cmets

    【讨论】:

    • 但是基于网站,作者提供了一个真实的例子,你可以在single.php中调用循环:/ 那为什么不呢?我不认为这是一个问题,我只是检查了我手动将视图计数设置为 >100 的帖子,并且功能正常工作,视图计数正在更新。它只是不适用于最新发布的新闻,这就是我正在经历的问题
    猜你喜欢
    • 2018-05-04
    • 1970-01-01
    • 1970-01-01
    • 2015-10-09
    • 2015-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多