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

WordPress自定义文章类型分页第2页404问题求助

Fixing 404 Error on Custom Post Type (Journal) Pagination Links

Hey there, let's work through this pagination 404 issue you're facing with your journal custom post type. This is a common snag with CPTs, and it usually ties back to rewrite rules, CPT registration settings, or small inconsistencies in your query code. Let's break down the fixes step by step:

1. Double-Check Your CPT Registration Settings

Since you used CPT UI to create the journal post type, head back to the plugin's settings and verify these key options:

  • Has Archive: Make sure this is set to True—this tells WordPress to generate a proper archive structure for your CPT, which pagination relies on.
  • Rewrite Slug: Confirm the slug is set to a clean value (like journal), and ensure the "With Front" box is checked (unless you intentionally want to exclude the front portion of your permalinks).

After adjusting these, flush your rewrite rules: Go to WordPress Dashboard → Settings → Permalinks, then just click "Save Changes" (no need to edit anything—this triggers a refresh of WordPress's URL mapping rules, which is critical for new pagination links to work).

2. Clean Up Your WP_Query Parameters

In your code, you’re using both showposts and posts_per_page—showposts is a deprecated parameter, and having both can cause conflicts. Remove 'showposts' => 2, from your $args array, leaving only 'posts_per_page' => 2, to define how many posts load per page.

The base parameter in your paginate_links() function might be misconfigured, especially if this code lives in a custom page template. Replace the hardcoded base with a WordPress-recommended dynamic approach:

Instead of:

'base' => get_pagenum_link( 1 ) . '%_%',

Use this:

'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),

This method dynamically generates the correct base URL for pagination, avoiding issues with different permalink structures.

4. Updated Working Code

Here’s your modified code with all fixes applied (I also fixed a typo in img-responsiv → img-responsive):

<div class="row"> 
<?php 
/** 
 * @package Momentum 
 * Loop custom post type (journal) 
 */ 

/** 
 * Pagination 
 */
$paged = (get_query_var( 'paged' )) ? get_query_var( 'paged' ) : 1; 
$args = array( 
    'post_type' => 'journal', 
    'posts_per_page' => 2, 
    'order' => 'DESC', 
    'paged' => $paged, 
); 
$loop = new WP_Query($args); 

if ($loop->have_posts()): 
    while ($loop->have_posts()) : $loop->the_post(); ?> 
    <div class="col-lg-6 content"> 
        <div class="article"> 
            <div class="image"> 
                <?php 
                /** 
                 * Check the featured image 
                 */
                if (has_post_thumbnail()) { 
                    the_post_thumbnail( 'full', array('class' => 'img-responsive') ); 
                } else { ?> 
                    <img src="<?php echo get_template_directory_uri(); ?>/assets/img/journal-1.jpg" alt="journal"> 
                <?php } ?> 
            </div> 
            <div class="text-content"> 
                <div class="head-content"> 
                    <div class="title"> 
                        <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3> 
                    </div> 
                    <div class="excerpt"> 
                        <?php the_excerpt(); ?> 
                    </div> 
                </div> 
                <div class="footer"> 
                    <a href="<?php the_permalink(); ?>" class="read-more pull-left">READ ENTRY</a> 
                    <span class="date pull-right"><?php echo get_the_date(); ?></span> 
                </div> 
            </div> 
        </div> 
    </div><!-- /.col-lg-6 --> 
    <?php endwhile; ?> 

    <?php 
    $total_pages = $loop->max_num_pages; 
    if ($total_pages > 1) { 
        $current_page = max(1, get_query_var( 'paged' )); 
        echo paginate_links( array( 
            'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
            'format' => 'page/%#%', 
            'current' => $current_page, 
            'total' => $total_pages, 
            'prev_text' => __('PREVIOUS'), 
            'next_text' => __('NEXT'), 
        ) ); 
    } ?> 

<?php else: ?> 
    <h1>No Journal found</h1> 
<?php endif; ?> 
<?php wp_reset_postdata(); ?> 
</div>

5. Final Check (If Issues Linger)

If you’re using a custom page template (not the default CPT archive), ensure you’re not overriding the main query in a way that conflicts with pagination. Also, confirm your permalink structure is set to anything other than "Plain" (Dashboard → Settings → Permalinks)—plain permalinks don’t support pretty URLs like /page/2/.

Give these steps a go, and your pagination links should start loading without 404 errors!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:09:38