无法使用自定义查询获取PAGINATE_LINKS

时间:2014-04-05 作者:rkoller

我在Wordpress网站上建立了一个新闻部分,其中包含高级自定义字段和自定义帖子类型ui插件。在自定义新闻概述页面(不是标准的wordpress帖子)上,我想将每页的新闻数量限制为6条,并提供带有paginate\\u链接的导航。我构建了以下代码段:

<section class="news-main" role="main">
    <?php
        $args = array(
            \'post_type\' => \'news\',
            \'posts_per_page\' => \'6\'
        );
        $the_query = new WP_Query( $args );
        $temp_query = $wp_query;
        $wp_query = NULL;
        $wp_query = $the_query;
        $pagination = array(
            \'base\' => \'%_%\',
            \'format\' => \'?page=%#%\',
            \'total\' => $the_query->max_num_pages,
            \'prev_next\' => True,
            \'prev_text\' => __( \'<< Previous\' ),
            \'next_text\' => __( \'Next >>\' )
        );
        if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <article class="news-snippet">
            <header class="news-box">
                <h2 class="mus-hi slogan-head fancy"><span class="fancy-hook"><?php the_time( \'j. F Y\' ); ?></span></h2>
                <a href="<?php the_permalink(); ?>"><p class="bran-hn slogan-state closure"><?php the_title(); ?></p></a>
            </header>
            <div class="news-wrap">
                <p class="news-excerpt"><?php echo acf_excerpt( \'news_post\', 35, \' <span class="news-more-inbox">[...]</span>\' ); ?></p>
                <p class="bran-hn news-more"><a href="<?php the_permalink(); ?>">More &rarr;</a></p>
            </div>
        </article>
    <?php endwhile; ?>
        <?php wp_reset_postdata(); ?>
    <?php else: ?>
        <p>No entries</p>
    <?php endif; ?>
    <nav>
        <?php echo paginate_links( $pagination );
                $wp_query = NULL;
                $wp_query = $temp_query; ?>
    </nav>
</section>
首先定义wp\\u查询的参数,然后$the_query$wp_query 变量保存到临时变量中,用NULL重置,然后设置为$the_query 变量在最后一步中paginate_links 已设置。然后是输出新闻帖子的循环。循环完成后,调用paginate\\u links函数,最后调用$wp_query 已重置并写回临时$temp_query 变量

如果我有15篇博客文章,我的输出如下:

paginate_links layout

如果我将鼠标悬停在数字3上,则slug的结尾如下所示:

slug for page 3

现在的问题是,即使设置了上一个和下一个链接,也不会显示它们。虽然主要问题是给定的slug-如果我单击数字3,我会在404页面上着陆。wordpress不知道我被引导到哪里的“第3页”。提前谢谢Ralf

Update:

$args = array(
    \'post_type\' => \'news\',
    \'posts_per_page\' => \'1\'
);  
$the_query = new WP_Query( $args );
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $the_query;
$total_pages = $wp_query->max_num_pages;
if ( $total_pages > 1) {
    $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
    $current_page = new WP_Query(\'post_type=news&posts_per_page=1&paged=\' . $paged);
    $pagination = array(
        \'base\' => \'%_%\',
        \'format\' => \'?paged=%#%\',
        \'mid-size\' => 1,
        \'current\' => $current_page,
        \'total\' => $total_pages,
        \'prev_next\' => True,
        \'prev_text\' => __( \'<< Previous\' ),
        \'next_text\' => __( \'Next >>\' )
    );
}
更新2:

好了,对代码的第一部分做了以下修改,我让导航正常工作了

$args = array(
    \'post_type\' => \'news\',
    \'posts_per_page\' => \'3\',
    \'paged\' => get_query_var( \'paged\' )
);  
$the_query = new WP_Query( $args );
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $the_query;
$total_pages = $wp_query->max_num_pages;
if ( $total_pages > 1) {
    $the_paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
    $pagination = array(
        \'base\' => @add_query_arg(\'paged\',\'%#%\'),
        \'format\' => \'?paged=%#%\',
        \'mid-size\' => 1,
        \'current\' => $the_paged,
        \'total\' => $total_pages,
        \'prev_next\' => True,
        \'prev_text\' => __( \'<< Previous\' ),
        \'next_text\' => __( \'Next >>\' )
    );
}
我剩下的唯一一个问题(唯一一个仍然不正常的问题)是第一个链接是否有可能是link而不是link/?分页=1

1 个回复
最合适的回答,由SO网友:Milo 整理而成

关于显示的页数,请通读paginate_links, 明确地end_sizemid_size.

至于404,问题是没有第3页。页面是否返回内容或加载404模板取决于主查询的结果,而不是在模板中运行的自定义查询。如果这是page 帖子类型,请尝试设置paged 查询变量而不是page.

结束

相关推荐

Pagination problem

我有两个自定义重写规则:add_rewrite_rule(\'foo/bar/?\', \'index.php?post_type=foo\', \'top\'); add_rewrite_rule(\'foo/bar/([a-z]+)/?\', \'index.php?post_type=foo&bar=$matches[1]\', \'top\'); 这给了我匹配自定义变量的帖子bar. 然而,我也想处理分页,因此当一个数字成功时/bar/ wordpress应将其视为页码。