get_next_posts_link()
云策文档标注
概述
get_next_posts_link() 函数用于检索当前查询中下一组文章的链接,通常指向较旧的文章(按时间倒序排列)。它接受可选参数来自定义链接标签和最大页数,并返回 HTML 格式的链接字符串。
关键要点
- 函数返回 HTML 格式的下一篇文章页面链接,用于分页导航。
- 参数 $label 可选,用于指定链接文本,默认为 null(使用默认翻译文本)。
- 参数 $max_page 可选,指定最大页数,默认为 0(自动从 $wp_query 获取)。
- 函数内部依赖全局变量 $paged 和 $wp_query 来确定当前页和总页数。
- 与 get_previous_posts_link() 配合使用,实现前后导航;通常 get_next_posts_link() 指向较旧文章,get_previous_posts_link() 指向较新文章。
- 提供过滤器 next_posts_link_attributes 用于修改链接的锚点属性。
代码示例
// 默认用法
echo get_next_posts_link();
// 自定义标签
echo get_next_posts_link( __( 'Go to next page', 'textdomain' ) );
// 自定义标签和最大页数
echo get_next_posts_link( __( 'Go to next page', 'textdomain' ), 4 );
// 在自定义 WP_Query 中使用
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
the_title();
endwhile;
echo get_next_posts_link( __( 'Older Entries', 'textdomain' ), $the_query->max_num_pages );
echo get_previous_posts_link( __( 'Newer Entries', 'textdomain' ) );
wp_reset_postdata();
endif;注意事项
- 在自定义 WP_Query 中,必须传递 $max_page 参数(如 $the_query->max_num_pages)以确保正确计算分页。
- 函数仅在非单篇文章页面且存在下一页时输出链接,否则返回空。
- 使用后应清理查询数据,例如通过 wp_reset_postdata() 恢复全局 $post 变量。
原文内容
Retrieves the next posts page link.
Parameters
$labelstringoptional-
Content for link text.
Default:
null $max_pageintoptional-
Max pages. Default 0.
Source
function get_next_posts_link( $label = null, $max_page = 0 ) {
global $paged, $wp_query;
if ( ! $max_page ) {
$max_page = $wp_query->max_num_pages;
}
if ( ! $paged ) {
$paged = 1;
}
$next_page = (int) $paged + 1;
if ( null === $label ) {
$label = __( 'Next Page »' );
}
if ( ! is_single() && ( $next_page <= $max_page ) ) {
/**
* Filters the anchor tag attributes for the next posts page link.
*
* @since 2.7.0
*
* @param string $attributes Attributes for the anchor tag.
*/
$attr = apply_filters( 'next_posts_link_attributes', '' );
return sprintf(
'<a href="%1$s" %2$s>%3$s</a>',
next_posts( $max_page, false ),
$attr,
preg_replace( '/&([^#])(?![a-z]{1,8};)/i', '&$1', $label )
);
}
}
Hooks
- apply_filters( ‘next_posts_link_attributes’, string $attributes )
-
Filters the anchor tag attributes for the next posts page link.
Changelog
| Version | Description |
|---|---|
| 2.7.0 | Introduced. |
Skip to note 5 content
Codex
Usage when querying the loop with WP_Query
Pass the
$max_pageparameter to theget_next_posts_link()function when querying the loop withWP_Query. To get the total amount of pages you can use themax_num_pagesproperty of the customWP_Queryobject.have_posts() ) : // The Loop while ( $the_query->have_posts() ) : $the_query->the_post(); the_title(); endwhile; // get_next_posts_link() usage with max_num_pages. echo get_next_posts_link( __( 'Older Entries', 'textdomain' ), $the_query->max_num_pages ); echo get_previous_posts_link( __( 'Newer Entries', 'textdomain' ) ); // Clean up after our custom query. wp_reset_postdata(); else : ?> <p></p>Skip to note 6 content
Codex
Default Usage
echo get_next_posts_link();Skip to note 7 content
Codex
Custom Label
echo get_next_posts_link( __( 'Go to next page', 'textdomain' ) );Skip to note 8 content
Codex
Custom Label and Custom number of post pages
echo get_next_posts_link( __( 'Go to next page', 'textdomain' ), 4 );