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

WordPress自定义分类模板分页失效,点击分页出现页面未找到错误

解决WordPress自定义分类归档分页404错误

你的问题核心在于用自定义页面模板(带Template Name标识)处理自定义分类归档,WordPress的重写规则无法识别这类页面的分页URL,导致点击分页时返回404。以下是具体修复方案:

步骤1:改用WordPress原生分类模板结构

WordPress有内置模板层级,针对自定义分类birth_month_day,你需要创建名为taxonomy-birth_month_day.php的模板文件(替换原自定义页面模板),放在主题根目录下。这样WordPress会自动为该分类的归档页面加载此模板,分页规则也能正常生效。

步骤2:修正模板代码

替换原模板代码为以下适配原生分类逻辑的版本:

<?php
/**
 * Template for birth_month_day taxonomy archive
 */

get_header();
$current_term = get_queried_object();
?>

<div class="container">
    <h1 class="birthday-heading-data"><?php echo esc_html($current_term->name); ?> Birthdays</h1>

    <?php 
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array(
        'post_type'      => 'celebrity',
        'posts_per_page' => 3,
        'paged'          => $paged,
        'tax_query'      => array(
            array(
                'taxonomy' => 'birth_month_day',
                'field'    => 'term_id', // 用term_id查询更可靠,避免slug冲突
                'terms'    => $current_term->term_id,
            ),
        ),
    );

    $query = new WP_Query($args);

    if ($query->have_posts()) : ?>
        <div class="birthday-grid">

            <?php while ($query->have_posts()) : $query->the_post(); ?>
                <?php
                $birth_month_day_terms = get_the_terms(get_the_ID(), 'birth_month_day');
                if ($birth_month_day_terms && !is_wp_error($birth_month_day_terms)) :
                    $occupation = get_the_terms(get_the_ID(), 'occupation');
                    $birth_year_terms = get_the_terms(get_the_ID(), 'birth_year');
                    $birth_year = $birth_year_terms && !is_wp_error($birth_year_terms) ? $birth_year_terms[0]->name : null;
                    $death_date = get_post_meta(get_the_ID(), '_celebrity_death_date', true);
                    $age = '';
                    
                    if (!empty($birth_year)) {
                        if (empty($death_date)) {
                            $age = ', ' . (date('Y') - intval($birth_year));
                        } else {
                            $death_year = date('Y', strtotime($death_date));
                            $age = ' (' . intval($birth_year) . '-' . $death_year . ')';
                        }
                    } else {
                        $age = ', N/A';
                    }
                    
                    $image = get_the_post_thumbnail(get_the_ID(), 'medium', ['class' => 'birthday-grid-card-image']);
                    $link = get_permalink();
                ?>
                    <div class="birthday-grid-card">
                        <a href="<?php echo esc_url($link); ?>">
                            <?php echo $image ?: '<div class="birthday-card-placeholder">No Image</div>'; ?>
                            <h3><?php echo get_the_title(); ?><?php echo esc_html($age); ?></h3>
                            <p><?php echo ($occupation && !is_wp_error($occupation)) ? esc_html($occupation[0]->name) : 'N/A'; ?></p>
                        </a>
                    </div>
                <?php endif; ?>
            <?php endwhile; ?>

        </div>

        <!-- Pagination -->
        <div class="pagination-wrapper">
            <?php
            $pagination_links = paginate_links(array(
                'base'      => get_term_link($current_term) . 'page/%#%/', // 基于分类术语链接构建分页base
                'format'    => '',
                'current'   => max(1, $paged),
                'total'     => $query->max_num_pages,
                'prev_text' => '&laquo; Previous',
                'next_text' => 'Next &raquo;',
                'type'      => 'list',
            ));

            if ($pagination_links) {
                echo str_replace('page-numbers', 'page-numbers generatepress', $pagination_links);
            }
            ?>
        </div>

    <?php else : ?>
        <p>No results found.</p>
    <?php endif; wp_reset_postdata(); ?>

</div>

<?php get_footer(); ?>

步骤3:刷新固定链接规则

修改模板后,登录WordPress后台进入设置 → 固定链接,直接点击「保存更改」按钮(无需修改任何设置),这会刷新WordPress的重写规则,让分页URL能被正确识别。

额外优化说明

  • 替换get_query_var('term')为$current_term->term_id,避免slug重复导致的查询错误
  • 增加!is_wp_error()判断,防止术语查询失败时出现PHP警告
  • 重构年龄计算逻辑,提升代码可读性和健壮性

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 20:13:14