代码之家  ›  专栏  ›  技术社区  ›  Harley Liang

为什么后视图计数功能不工作?

  •  0
  • Harley Liang  · 技术社区  · 6 年前

    很好的一天。我一直在研究如何根据 this 链接

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

    my 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();?>
    
    2 回复  |  直到 6 年前
        1
  •  0
  •   Vel    6 年前

    尝试此代码。

    function subh_set_post_view($postid)
    {
    
    $count ou key='post ou views ou count';
    $count=(int)获取\u post\u元($postid,$count\u键,真);
    
    如果($count==0){
    计数+ +;
    删除\u post_meta($postid,$count_key);
    添加_post_meta($postid,$count_key,$count++);
    }否则{
    计数+ +;
    更新_post_meta($postid,$count_key,$count);
    }
    
    }
    
    函数getPostView($postId){
    $count ou key='post ou views ou count';
    $count=get_post_meta($postid,$count_key,true);
    返回$count。“视图”;
    }
    
    /**
    *在“管理面板文章”列表中添加新列
    *
    *@参数$默认值
    *
    *@返回混合
    */
    函数subh_posts_column_views($defaults)
    {
    $defaults['post_views']=uu('views');
    
    返回$defaults;
    }
    
    /**
    *显示管理面板上每个日志的视图数
    *
    *@参数$列名称
    *@ PARAM$ID
    *
    *@返回void只返回视图数
    */
    函数subh_posts_custom_column_views($column_name,$id)
    {
    如果($column_name=='发布视图'){
    echo getPostView(get_the_id());
    }
    }
    
    添加_filter('manage_posts_columns'、'subh_posts_column_views');
    添加\action('manage_posts_custom_column'、'subh_posts_custom_column_views',5,2);
    < /代码> 
    
    

    enter image description here

        2
  •  0
  •   I am the Most Stupid Person    6 年前

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

    这就是你的功能失效的原因。你可以使用 GETHythiId() 只有在循环内部,不能在single.php上使用它。

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