0

我在将下一个和上一个链接添加到单个帖子时遇到问题(我不想要下一个帖子,我需要下一组帖子)。

该网站的工作方式是,当前帖子显示在页面顶部,下面显示所有相关帖子的列表,并带有分页。显示相关帖子的查询是:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 

$args = array(
    'category__in'   => $search_categories,
    'posts_per_page' => 4,
    'paged'          => $paged
);

$search_term = null;

if ( isset( $_GET['search_term'] ) ) {
    $args['s'] = esc_attr( $_GET['search_term'] );
}

$twp_query = new WP_Query( $args );

我知道查询是返回足够的帖子。

这是循环(剥离所有标记)

if ( $twp_query->have_posts() ) {

    while ( $twp_query->have_posts() ) {

        $twp_query->the_post();

        get_template_part( 'post', 'listitem' );

    }

    next_posts_link('Next', $twp_query->max_num_pages);
    previous_posts_link('Previous ', $twp_query->max_num_pages);

} else {

    _e( 'Apologies, but no results were found.', 'rep' );

}

它显示返回的前 4 个项目,但不显示下一个和上一个帖子链接。

我会很感激任何帮助,因为这个让我很难过!

更新:

刚刚尝试将 'nopageing' => false 添加到 args 数组,没有运气。

另一个更新:

在 wp-includes/link-template.php 中查看了 WordPress 的 get_next_posts_link,从这段代码来看,这是不可能的。除非其他人知道不同?

if ( !is_single() && ( $nextpage <= $max_page ) ) {
    $attr = apply_filters( 'next_posts_link_attributes', '' );
    return '<a href="' . next_posts( $max_page, false ) . "\" $attr>" . preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $label) . '</a>';
}
4

1 回答 1

0

然后我将自己回答这个问题,由于 WordPress 的核心,这是不可能的,任何返回 true 到 is_single 的页面都被阻止显示下一篇和上一篇文章链接,一个例子:

if ( !is_single() && ( $nextpage <= $max_page ) ) {
    $attr = apply_filters( 'next_posts_link_attributes', '' );
    return '<a href="' . next_posts( $max_page, false ) . "\" $attr>" . preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $label) . '</a>';
}
于 2012-11-16T13:19:15.673 回答