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

如何显示自定义Post Type所有内容?解决404页面未找到问题

解决自定义Post Type的404错误及正常显示问题

一、先解决最常见的404原因:刷新重写规则

WordPress注册自定义帖子类型后,不会自动更新重写规则,这是导致404的头号原因。操作步骤:

  1. 登录WordPress后台,进入「设置」→「固定链接」
  2. 随便切换到另一种固定链接格式(比如「朴素」),点击「保存更改」
  3. 再切换回你原来使用的格式(比如「帖子名称」),再次点击「保存更改」

这一步会强制WP刷新重写规则,让新的campaign自定义帖子类型的链接生效。

二、修复自定义帖子类型注册代码的缺失参数

你的fp_create_campaign_post_type函数里缺少关键的rewrite配置,导致WP无法生成正确的归档和单页链接。修改后的代码如下:

function fp_create_campaign_post_type() {
    register_post_type('campaign',
        array(
            'labels' => array(
                'name' => __('Campagnes'),
                'singular_name' => __('Campagne'),
                'add_new' => __('Ajouter une campagne'),
                'add_new_item' => __('Ajouter une campagne'),
                'edit_item' => __('Modifier la campagne'),
                'new_item' => __('Nouvelle campagne'),
                'view_item' => __('Voir la campagne'),
                'search_items' => __('Rechercher parmi les campagnes'),
                'not_found' => __('Aucune campagne trouvée'),
                'not_found_in_trash' => __('Aucune campagne trouvée dans la corbeille')
            ),
            'public' => true,
            'has_archive' => 'campaign', // 归档页面的slug
            'rewrite' => array(
                'slug' => 'campaign', // 自定义帖子类型的链接前缀
                'with_front' => false // 避免继承主题的前置路径(比如/blog/)
            ),
            'supports' => array('title', 'editor', 'thumbnail'),
            'publicly_queryable' => true, // 确保可以通过查询访问
            'show_in_rest' => true // 可选,支持Gutenberg编辑器
        )
    );
}
add_action('init', 'fp_create_campaign_post_type');

三、使用正确的模板文件实现归档和单页显示

你当前用的template-campaign.php是页面模板,需要手动创建页面并选择该模板才能使用,这不是WP自定义帖子类型的标准归档方式。正确的做法是使用WP默认的模板层级:

1. 归档页面(显示所有活动)

创建archive-campaign.php文件,内容可以参考你的template-campaign.php修改:

<?php
get_header();

// 查询所有已发布的campaign
$args = array(
    'post_type' => 'campaign',
    'post_status' => 'publish',
    'posts_per_page' => -1,
);

$query = new WP_Query($args);

if ($query->have_posts()) :
    while ($query->have_posts()) : $query->the_post();
        ?>
        <article class="campaign-item">
            <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
            <?php echo fp_get_campaign_progress_bar(get_the_ID()); ?>
            <?php the_excerpt(); // 可选显示摘要 ?>
        </article>
        <?php
    endwhile;
    wp_reset_postdata();
else :
    echo '<p>Aucune campagne trouvée</p>';
endif;

get_footer();
?>

现在访问你的域名/campaign/就能直接看到所有活动,无需手动创建页面。

2. 单页(显示单个活动)

创建single-campaign.php文件,用来显示单个活动的详细内容:

<?php
get_header();

if (have_posts()) :
    while (have_posts()) : the_post();
        ?>
        <article class="campaign-single">
            <h1><?php the_title(); ?></h1>
            <?php the_post_thumbnail(); // 显示特色图片 ?>
            <?php echo fp_get_campaign_progress_bar(get_the_ID()); ?>
            <?php the_content(); // 显示正文内容 ?>
        </article>
        <?php
    endwhile;
endif;

get_footer();
?>

现在点击活动标题的链接,就能正常访问单个活动页面,不会出现404。

四、修复短代码的错误

你的custom_post_type_list_shortcode里有个变量错误:$type = 'custom_post_type'; 应该改为$type = 'campaign';,否则短代码无法查询到任何活动内容。修改后的短代码函数:

function custom_post_type_list_shortcode() {
    ob_start();

    $type = 'campaign'; // 这里改为你的自定义帖子类型slug
    $args = array(
        'post_type' => $type,
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'ignore_sticky_posts' => true
    );
    $my_query = new WP_Query($args);

    if ($my_query->have_posts()) :
        echo '<ul>';
        while ($my_query->have_posts()) : $my_query->the_post();
            echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
        endwhile;
        echo '</ul>';
    else :
        echo '<p>Aucune publication trouvée.</p>';
    endif;

    wp_reset_postdata();

    return ob_get_clean();
}
add_shortcode('custom_post_type_list', 'custom_post_type_list_shortcode');

最后验证

完成以上步骤后:

  1. 再次刷新固定链接(确保重写规则生效)
  2. 访问你的域名/campaign/查看归档页面
  3. 点击单个活动的链接查看单页
  4. 在页面中插入[custom_post_type_list]短代码,验证列表是否正常显示

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 20:04:52