参数
- $label
-
(string) (可选) 下一页链接文本。
默认值: null
- $max_page
-
(int) (可选) 最大页数,默认值为0。
返回
(string|void) 下一页文章页面链接的HTML格式
说明
获取当前查询下一组文章的链接。
由于文章查询通常按相反的时间顺序排序,get_next_posts_link()通常指向较旧的条目(朝向集合的末尾),get_previous_posts_link()通常指向较新的条目(朝向集合的开头)。
源码
更新日志
版本 | 描述 |
---|---|
2.7.0 | 开始引入 |
使用示例
使用WP_Query查询循环时的用法
使用
WP_Query
查询循环时,将$max_page
参数传递给get_next_posts_link()
函数。要获得总页数,可以使用自定义WP_Query
对象的max_num_pages
属性。<?php /** * Set the "paged" parameter (use 'page' if the query is on a static front page). * * @var int $paged */ $paged = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1; /** @var WP_Query $the_query */ $the_query = new WP_Query( 'cat=1&paged=' . $paged ); if ( $the_query->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><?php _e( 'Sorry, no posts matched your criteria.', 'textdomain' ); ?></p> <?php endif; ?>
默认用法
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 );