Wordpress next_posts_link 链接错误 URL

Wordpress next_posts_link Linking to wrong URL

我在 single.php 上设置了 next_posts_link,它生成以下 URL:

http://mywebsite.com/news/article1/page/2

但是,此 url 将被重定向到

http://mywebsite.com/news/article1

有什么方法可以进入第二页吗?

这似乎是 Wordpress 永久链接的问题。我目前使用自定义永久链接结构

/%category%/%postname%/

将永久链接设置为默认链接可解决此问题,但此项目需要自定义永久链接。

如果您希望您的长 post 显示 2 或 3 页。您只需在 post 中添加 <!--nextpage--> 。其后的内容将在下一页显示。 Reference

您应该使用 next_post_link(),这意味着在单个 post 之间导航。 next_posts_link 在页面之间导航

如果您需要导航分页 post(使用 <--nextpage-->),那么您应该使用 wp_link_pages

编辑

我最近在 WPSE 上做了同样的事情。正如我在 post 中解释的那样,您需要的结构不适用于默认 permalink 结构之外的任何 permalink 结构。

在粘贴该答案之前请注意,我已经为 /%postname%/ permalink 结构完成了该操作。只需将 /%postname%/ 的所有实例更改为适当的结构

POST 来自 WPSE

正如我所说,您所追求的整个设置不可能在原生环境中使用 pretty permalinks。您的设置可能使用默认的 permalink 结构,因为两个查询(主查询和您的自定义查询)以相同的方式读取这些 permalink。当您切换到 pretty permalinks 时,单个页面上的两个查询对 URL 的解释不同,导致在您尝试对自定义查询

进行分页时一个或另一个失败

单页永远不会以这种方式分页,特别是使用漂亮的 permalinks。我已经尝试了几个想法,实现这个的最好方法是

  • 编写自己的分页函数,可以从URL

  • 中读取页码
  • 编写自己的函数,可以将页码附加到 URL,类似于将 /2/ 添加到单页的 URL。

我必须强调,如果您使用 <!--nextpage--> 对单个 post 进行分页,您的 post 也会与您的自定义查询一起分页。此外,如果您的 permalink 结构未设置为 /%postname%/,代码将失败并通过 wp_die()

显示错误消息

代码

这是获取 next/previous 页面并将页码添加到 URL 的代码。

function get_single_pagination_link( $pagenum = 1 ) {
    global $wp_rewrite;

    if( is_singular() && $wp_rewrite->permalink_structure == '/%postname%/') {

        $pagenum = (int) $pagenum;

        $post_id = get_queried_object_id();
        $request = get_permalink( $post_id );

        if ( $pagenum > 1 ) {
            $request = trailingslashit( $request ) . user_trailingslashit( $pagenum );
        }

        return esc_url( $request );

    }else{

        wp_die( '<strong>The function get_single_pagination_link() requires that your permalinks are set to /%postname%/</strong>' );

    }
}

在对自定义查询进行分页时,您可以按如下方式使用此函数来获取单个页面中任何页面的link

get_single_pagination_link( 'pagenumber_of_previous_or_next_page' );

同样,正如我所说,没有分页功能可以对您的自定义查询进行分页或从 URL 读取页码,因此您必须自己编写。

这是我为自定义查询中的下一页和上一页创建 link 的想法。如果你仔细观察,你会看到我是如何使用之前声明的函数 get_single_pagination_link() 来让 link 进入下一页和上一页

function get_next_single_page_link ( $label = null, $max_page = 0 ) {
    global $wp_query;

    if ( !$max_page ) {
        $max_page = $wp_query->max_num_pages;
    }

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

    if( is_singular() ) {

        $next_page = intval($paged) + 1;

        if ( null === $label ) {
            $label = __( 'Next Page &raquo;' );
        }

        if ( ( $next_page <= $max_page ) ) {
            return '<a href="' . get_single_pagination_link( $next_page ) . '">' . $label . '</a>';
        }

    }
}

function get_previous_single_page_link( $label = null ) {

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

    if( is_singular() ) {

        $prev_page = intval($paged) - 1;

        if ( null === $label ) {
            $label = __( '&laquo; Previous Page' );
        }

        if ( ( $prev_page > 0 ) ) {
            return '<a href="' . get_single_pagination_link( $prev_page ) . '">' . $label . '</a>';
        }

    }

}

您现在可以在代码中使用这两个函数来对自定义查询进行分页。这两个函数的工作方式与 get_next_posts_link() and get_previous_posts_link() 完全相同,并使用完全相同的参数,因此您需要记住,您需要为自定义查询

传递 $max_page 参数

这是您使用这些函数的自定义查询。

function get_related_author_posts() {

    global $authordata, $post;

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

    $args = array( 
        'posts_per_page'    => 2,
        'paged'             => $paged
    );
    $authors_posts = new WP_Query( $args );

    $output = '';

    if( $authors_posts->have_posts() ) {

        $output = '<ul>';

        while( $authors_posts->have_posts() ) {
            $authors_posts->the_post();

            $output .= '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a>' . get_the_excerpt() . '</li>';

        }

        $output .= '</ul>';

        $output .= get_previous_single_page_link();
        $output .= get_next_single_page_link( null , $authors_posts->max_num_pages );

        wp_reset_postdata();
    }

    return $output;

}