当前浏览:首页 / WordPress函数 / next_posts_link()

next_posts_link( string $label = null, int $max_page )

显示下一页文章页面链接

linkmore...

next 下一...

postsmore...


参数

$label

(string) (可选) 链接文本的内容。

默认值: null

$max_page

(int) (可选) 最大页数,默认值为0。


说明

此函数用于打印指向当前查询中下一组文章的链接
如果需要在PHP中使用的值,请使用get_next_posts_link()
由于文章查询通常按相反的时间顺序排序,next_posts_link()通常指向较旧的条目(朝向集合的末尾),而previous_posts_link()通常指向较新的条目(朝向集合的开头)。

参数$max_pages是显示链接的页数限制。默认值“0”表示“无限制”。

如果出现以下情况,此函数将不工作(无声失败)如果 mysql.trace_mode 在您的 php.ini 中被启用。如果无法编辑该文件,请尝试添加 ini_set( 'mysql.trace_mode', 0 ); 到您主题的 functions.php

另请参阅:previous_posts_link()next_posts_link()



源码

查看源码 官方文档


更新日志

版本描述
0.71开始引入

使用示例

  • 示例1

    使用WP_Query查询循环时的用法

    当用WP_Query查询循环时,在next_posts_link()函数中加入$max_pages参数。要获得总页数,你可以使用自定义WP_Query对象的'max_num_pages'属性。

    // set the "paged" parameter (use 'page' if the query is on a static front page)
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    
    // the query
    $the_query = new WP_Query( array(
    	'cat'   => 1,
    	'paged' => $paged
    );
    
    if ( $the_query->have_posts() ) :
    	// the loop
    	while ( $the_query->have_posts() ) : $the_query->the_post();
    		the_title();
    		
    	endwhile;
    
    	// next_posts_link() usage with max_num_pages.
    	next_posts_link( __( 'Older Entries', 'textdomain' ), $the_query->max_num_pages );
    	previous_posts_link( __( 'Newer Entries', 'textdomain' ) );
    
    	// Clean up after the query and pagination.
    	wp_reset_postdata(); 
    
    else:
    	?>
    	<p><?php _e( 'Sorry, no posts matched your criteria.', 'textdomain' ) ); ?></p>
    	<?php
    endif;
    
  • 示例2

    基本示例

    <?php next_posts_link( 'Older Entries »', 0 ); ?>
    
  • 示例3

    检查下一个链接是否存在

    if ( get_next_posts_link() ) :
    	next_posts_link( 'Older Entries »', 0 );
    endif;