You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Dooplay主题中展示最高浏览量文章及白屏问题解决

问题原因

  • 统计逻辑与查询的文章类型不匹配:你在functions.php中只统计了watch类型文章的访问量,但模板中查询的是movies类型,导致movies类型没有views元数据,查询无结果
  • 模板查询逻辑错误:定义好$query参数后,你额外创建了$popularloop对象,但传入的$movies变量未定义,该查询为空,嵌套循环无法输出内容
  • 统计函数存在小缺陷:首次访问文章时,仅将浏览量设为0,不会计入本次访问

修复步骤

1. 修正访问统计逻辑

修改functions.php中的追踪函数,覆盖你需要统计的所有文章类型:

function set_views($post_ID) {
    $key = 'views';
    $count = get_post_meta($post_ID, $key, true);

    if($count == ''){
        $count = 1; // 首次访问直接计为1,修复初始统计问题
        delete_post_meta($post_ID, $key);
        add_post_meta($post_ID, $key, $count);
    } else{
        $count++;
        update_post_meta($post_ID, $key, $count);
    }
}
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

function track_custom_post_watch ($post_ID) {
    // 新增movies类型到统计范围,可根据需求增减
    if ( !is_singular( array('watch', 'movies') ) ) return; 

    if ( empty ( $post_ID) ) {
        global $post; 
        $post_ID = $post->ID;    
    }
    set_views($post_ID);
}
add_action( 'wp_head', 'track_custom_post_watch');

2. 修正模板查询与循环逻辑

替换模板中原来的查询和循环部分代码,移除无效的嵌套循环:

<h3>Popular Posts</h3>

<?php
$query = new WP_Query( array(
        'post_type'      => 'movies',
        'posts_per_page' => 10, 
        'meta_key'       => 'views',
        'orderby'        => 'meta_value_num',
        'order'          => 'DESC',
        // 新增过滤规则,匹配存在views元数据的文章
        'meta_query'     => array(
            array(
                'key'     => 'views',
                'value'   => 0,
                'compare' => '>='
            )
        )
    )
);

if ($query->have_posts()) :
    while ($query->have_posts()) : $query->the_post();
?>
    <a href="<?php the_permalink() ?>">
    <img src="<?php echo dbmovies_get_poster($post->ID); ?>" alt="<?php the_title(); ?>">
    </a>
    <a href="<?php the_permalink() ?>">
    <h3 class="rating"><?php the_title(); ?></h3>
    </a>
<?php
    endwhile;
    wp_reset_postdata();
else :
    // 无结果时输出提示,方便排查
    echo '<p>暂无热门内容</p>';
endif;
?>

3. 验证生效

修改完成后,先访问几篇已发布的movies类型文章触发浏览量统计,再打开自定义模板页面即可正常展示内容。

内容的提问来源于stack exchange,提问作者Muhammad Hassan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 11:57:01