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

静态页面带动态post_type参数时paginate_links分页失效如何解决?

问题根因
  • 自定义WP_Query未传入分页参数,每次查询默认返回第一页内容
  • 动态获取post_type的逻辑直接读取完整查询串,存在多参数时会将分页参数也误识别为post_type值
  • 分页生成配置未保留post_type筛选参数,切换页码后筛选条件丢失
  • 静态页面下默认的get_query_var('paged')无法正确读取页码
完整修复代码
<section class="resources-blocks">
    <div class="grid-container">
        <div class="grid-x grid-margin-x align-center" id="container">
            <?php
            // 修复1:规范获取post_type参数,避免多参数冲突
            // 此处默认参数名为post_type,对应URL格式为website.com/page/?post_type=xxx
            $resourcesPostTypes = isset($_GET['post_type']) ? sanitize_text_field($_GET['post_type']) : 'post';
            // 若要沿用原无参数名的URL格式(website.com/page/?xxx),替换为以下代码:
            // $query_arr = [];
            // parse_str($_SERVER['QUERY_STRING'], $query_arr);
            // unset($query_arr['paged'], $query_arr['page']);
            // $resourcesPostTypes = !empty($query_arr) ? key($query_arr) : 'post';

            // 修复2:兼容静态页面获取当前页码
            $paged = max(1, get_query_var('page') ? get_query_var('page') : get_query_var('paged'));

            $args = array(
                'post_type' => $resourcesPostTypes,
                'post_status' => array('publish'),
                'orderby' => 'date',
                'order' => 'DESC',
                'post__not_in'   => array(15552, 15554, 15555),
                'posts_per_page' => $posts_to_display,
                'paged' => $paged // 修复3:查询参数加入当前页码
            );

            $query = new WP_Query($args);
            $resourceCount = 0;

            if ($query->have_posts()) {
                while ($query->have_posts()) {
                    $query->the_post();
                    $resourceCount++;

                    $post_type = get_post_type();
                    $post_type_object = get_post_type_object($post_type);
                    if ($post_type_object) {
                        $post_type_display = esc_html($post_type_object->labels->singular_name);
                    }
                    ?>
                    <div class="cell small-12 medium-4 large-3">
                        <?php
                        $is_downloadable = get_field('is_downloadable');
                        $asset_redirect = get_field('asset_redirect');
                        if ($is_downloadable) {
                            $download_link = get_field('download_link');
                            $resource_url = $download_link['url'];
                        } else if ($asset_redirect) {
                            $resource_url = $asset_redirect;
                        } 
                        else {
                            $resource_url = get_permalink();
                        }
                        ?>
                        <a class="asset-block <?php echo ($post_type); ?>" href="<?php echo $resource_url; ?>">
                            <i class="icon-<?php echo $post_type; ?>"></i>
                            <p class="content-type"><?php echo ucfirst($post_type_display); ?></p>
                            <h4><?php echo wp_trim_words(get_the_title(), 20); ?></h4>
                        </a>
                    </div>
                    <?php
                }
            }
            ?>
            <div class="pagination">
                <?php 
                    echo paginate_links( array(
                        'base'         => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
                        'total'        => $query->max_num_pages,
                        'current'      => $paged,
                        'format'       => '?paged=%#%',
                        'show_all'     => false,
                        'type'         => 'plain',
                        'end_size'     => 2,
                        'mid_size'     => 1,
                        'prev_next'    => true,
                        'prev_text'    => sprintf( '<i></i> %1$s', __( 'Newer Posts', 'text-domain' ) ),
                        'next_text'    => sprintf( '%1$s <i></i>', __( 'Older Posts', 'text-domain' ) ),
                        'add_args'     => array('post_type' => $resourcesPostTypes), // 修复4:分页保留post_type筛选参数
                        'add_fragment' => '',
                    ) );
                ?>
            </div>
            <?php wp_reset_postdata(); ?>
        </div>
    </div>
</section>
注意事项

建议优先使用带参数名的URL格式website.com/page/?post_type=文章类型别名,兼容性和可维护性更高,也避免后续新增参数时出现逻辑冲突。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 23:54:04