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

自定义文章类型(Custom Post Type)分页实现遇阻求助

Fixing Pagination for Your "Obleke" Custom Taxonomy

Hey there! I totally get how frustrating it is when pagination won't cooperate with custom taxonomies—basic tutorials often skip over the tricky edge cases. Let's walk through getting this sorted for your porocne_obleke_custom_taxonomies setup.

First, let's make sure your taxonomy is registered correctly. It looks like your code snippet cuts off, so double-check you're including these critical parameters when calling register_taxonomy:

function porocne_obleke_custom_taxonomies() {
    $labels = array(
        "name" => "Obleke",
        "singular_name" => "Obleke",
        "search_items" => "Search Obleke",
        "all_items" => "All Obleke",
        "parent_item" => "Parent Obleke",
        "parent_item_colon" => "Parent Obleke:",
        "edit_item" => "Edit Obleke",
        "update_item" => "Update Obleke",
        "add_new_item" => "Add New Obleke",
        "new_item_name" => "New Obleke Name",
        "menu_name" => "Obleke"
    );

    $args = array(
        "labels" => $labels,
        "hierarchical" => true,
        "public" => true,
        "show_ui" => true,
        "show_admin_column" => true,
        "show_in_nav_menus" => true,
        "show_tagcloud" => true,
        // These two are key for pagination to work
        "rewrite" => array("slug" => "obleke", "with_front" => false),
        "query_var" => true
    );

    register_taxonomy("obleke", array("your_custom_post_type"), $args); // Replace with your CPT slug
}
add_action("init", "porocne_obleke_custom_taxonomies");

Option 1: Adjust the Main Query (Cleanest Approach)

Instead of creating a new WP_Query in your template, use the pre_get_posts hook to modify the main query directly. This way, WordPress's built-in pagination functions work out of the box.

Add this to your theme's functions.php:

function obleke_taxonomy_pagination($query) {
    // Only target the main query on your Obleke taxonomy archive, not admin pages
    if (!is_admin() && $query->is_main_query() && $query->is_tax("obleke")) {
        $query->set("posts_per_page", 6); // Set your desired posts per page
        $query->set("paged", get_query_var("paged") ? get_query_var("paged") : 1);
    }
}
add_action("pre_get_posts", "obleke_taxonomy_pagination");

Then in your taxonomy-obleke.php template, you can use the standard loop and pagination:

<?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
        <!-- Your post content markup here -->
        <h2><?php the_title(); ?></h2>
        <?php the_excerpt(); ?>
    <?php endwhile; ?>

    <!-- Pagination -->
    <div class="pagination">
        <?php the_posts_pagination(array(
            "mid_size" => 2,
            "prev_text" => "← Previous",
            "next_text" => "Next →"
        )); ?>
    </div>
<?php else : ?>
    <p>No posts found in this category.</p>
<?php endif; ?>

Option 2: Using a Custom WP_Query (If You Need It)

If you must use a custom query, you need to sync it with the global $wp_query so pagination functions recognize it. Here's how:

<?php
$paged = get_query_var("paged") ? get_query_var("paged") : 1;

$obleke_query = new WP_Query(array(
    "post_type" => "your_custom_post_type", // Replace with your CPT slug
    "tax_query" => array(
        array(
            "taxonomy" => "obleke",
            "field" => "slug",
            "terms" => get_query_var("obleke")
        )
    ),
    "posts_per_page" => 6,
    "paged" => $paged
));

if ($obleke_query->have_posts()) :
    while ($obleke_query->have_posts()) : $obleke_query->the_post();
        <!-- Your post content -->
        <h2><?php the_title(); ?></h2>
        <?php the_excerpt(); ?>
    <?php endwhile; ?>

    <!-- Pagination setup -->
    <div class="pagination">
        <?php
        // Temporarily replace global query with our custom one
        global $wp_query;
        $temp_query = $wp_query;
        $wp_query = $obleke_query;

        the_posts_pagination(array(
            "mid_size" => 2,
            "prev_text" => "← Previous",
            "next_text" => "Next →"
        ));

        // Restore original global query
        $wp_query = $temp_query;
        ?>
    </div>

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

Final Critical Step: Flush Rewrite Rules

After making these changes, go to Settings → Permalinks in your WordPress admin and click "Save Changes" (you don't need to modify anything). This flushes the rewrite rules and ensures WordPress recognizes your taxonomy's pagination URLs.

If you're still having issues, double-check that your custom post type is registered with publicly_queryable => true and that you're using the correct slug for both the CPT and taxonomy.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 03:24:39