next_posts_link() - WordPress 函数参考中文文档

返回介绍

next_posts_link()

发布于 2017-09-11 字数2882 浏览 776 评论 0

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

Displays the next posts page link.


描述


参数

$label

(string)
(Optional)
Content for link text.

Default value: null

$max_page

(int)
(Optional)
Max pages. Default 0.


源代码

File: wp-includes/link-template.php

function next_posts_link( $label = null, $max_page = 0 ) {
	echo get_next_posts_link( $label, $max_page );
}

更新日志

Version 描述
0.71 Introduced.

相关函数

Uses

  • wp-includes/link-template.php:
    get_next_posts_link()

User Contributed Notes

  1. Skip to note content

    You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note

    Contributed by Codex

    Basic Example

    
    <?php next_posts_link( 'Older Entries »', 0 ); ?>
    
  2. Check if next link exists

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

    Usage when querying the loop with WP_Query

    Add the $max_pages parameter to the next_posts_link() function when querying the loop with WP_Query. To get the total amount of pages you can use the ‘max_num_pages’ property of the custom WP_Query object.

    
    // 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;
    

上一篇:next_posts()

下一篇:next_post_link()

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!