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

Polylang插件问题:单篇文章关联多语言分类且不重复

解决Polylang多语言分类关联单篇文章不重复显示的问题

看起来你之前的修改因为硬编码了罗马尼亚语(ro)的逻辑,才导致切换语言后失效。咱们换个动态适配所有语言的方案,核心是利用Polylang自带的关联分类功能,统一查询所有对应多语言分类下的文章并去重,同时保留你的统一评分排序。

核心思路

  1. 动态获取当前分类的所有多语言关联分类ID(不管当前切换到哪种语言)
  2. 构建自定义查询,拉取这些分类下的所有文章,但确保同一篇文章只显示一次
  3. 保留原有的评分排序规则,替换掉模板里的默认主查询

完整的category.php修改方案

把你模板里原有的主查询部分替换成下面的代码,注意根据你实际的评分字段调整参数:

<?php get_header(); ?>

<div class="content-area">
    <?php
    // 获取当前页面的分类对象
    $current_category = get_queried_object();
    $target_cat_ids = array($current_category->term_id);

    // 仅当Polylang激活时,获取当前分类的所有多语言关联分类
    if (function_exists('pll_get_translations')) {
        $translated_terms = pll_get_translations('term', $current_category->term_id);
        foreach ($translated_terms as $translated_term) {
            $target_cat_ids[] = $translated_term->term_id;
        }
        // 去重,避免重复添加相同分类ID
        $target_cat_ids = array_unique($target_cat_ids);
    }

    // 构建查询参数:替换meta_key为你存储评分的自定义字段名
    $custom_query_args = array(
        'category__in'       => $target_cat_ids,
        'post_type'          => 'post',
        'posts_per_page'     => get_option('posts_per_page'),
        'paged'              => get_query_var('paged') ?: 1,
        // 按评分排序(示例:假设评分存在自定义字段`post_rating`)
        'orderby'            => 'meta_value_num',
        'meta_key'           => 'post_rating',
        'order'              => 'DESC',
        'distinct'           => true, // 强制文章唯一,避免重复显示
    );

    // 执行自定义查询
    $custom_post_query = new WP_Query($custom_query_args);
    ?>

    <?php if ($custom_post_query->have_posts()) : ?>
        <header class="page-header">
            <h1><?php single_cat_title(); ?></h1>
            <?php the_archive_description('<div class="taxonomy-description">', '</div>'); ?>
        </header>

        <?php while ($custom_post_query->have_posts()) : $custom_post_query->the_post(); ?>
            <!-- 这里是你的文章模板内容,示例: -->
            <article class="post-item">
                <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                <div class="post-rating">
                    <?php echo '评分:' . get_post_meta(get_the_ID(), 'post_rating', true); ?>
                </div>
                <?php the_excerpt(); ?>
            </article>
        <?php endwhile; ?>

        <!-- 分页导航 -->
        <?php
        the_posts_pagination(array(
            'total'   => $custom_post_query->max_num_pages,
            'current' => get_query_var('paged') ?: 1,
            'mid_size' => 2,
        ));
        ?>

    <?php else : ?>
        <p><?php _e('该分类下暂无文章', 'your-theme-text-domain'); ?></p>
    <?php endif; ?>

    <!-- 重置查询,避免影响后续模板内容 -->
    <?php wp_reset_postdata(); ?>
</div>

<?php get_sidebar(); ?>
<?php get_footer(); ?>

关键说明

  1. 动态关联多语言分类:用pll_get_translations函数自动获取当前分类的所有多语言版本ID,不管你切换到英语、法语还是其他语言,都会自动匹配对应的关联分类,解决了你之前只在ro语言生效的问题。
  2. 避免文章重复:查询参数里的distinct => true会强制MySQL返回唯一的文章记录,哪怕同一篇文章属于多个关联分类。
  3. 保留评分排序:你只需要把meta_key的值替换成你实际存储评分的自定义字段名即可,如果用的是评分插件(比如WP-PostRatings),对应的字段名可能是ratings_average,需要根据插件文档调整。
  4. 兼容性保障:加了function_exists('pll_get_translations')判断,万一Polylang未激活,模板会自动 fallback 到默认的单分类查询。

额外注意事项

  • 确保你已经在Polylang后台正确关联了各语言的分类:进入分类编辑页,右侧Polylang模块里选择对应语言的分类即可。
  • 测试不同语言的分类页,确认所有关联分类下的文章都能正常显示,且没有重复。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 10:03:11