get_next_posts_link()调用同分类“下一页”分页的链接

目录

描述

Gets a link to the previous set of posts within the current query.

Because post queries are usually sorted in reverse chronological order, get_next_posts_link() usually points to older entries (toward the end of the set) and get_previous_posts_link() usually points to newer entries (toward the beginning of the set).

Note: See Troubleshooting Broken Pagination if you have pagination issues.

get_next_posts_link() 描述

用法

 <?php echo get_next_posts_link$label$max_page ); ?> 

get_next_posts_link() 用法

参数

$label

(string) (可选) The content for the link text

默认值: Next Page »

$max_page

(int) (可选) Limit the number of pages on which the link is displayed. The default value "0" means "no limit".

默认值: 0

get_next_posts_link() 参数

返回值

Link to next page if successful, otherwise null.

get_next_posts_link() 返回值

示例

Default Usage

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php echo get_next_posts_link(); ?>

Custom Label

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php echo get_next_posts_link('Go to next page'); ?>

Custom Label and Custom number of post pages

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php echo get_next_posts_link('Go to next page',4); ?>

Usage when querying the loop with WP_Query

Add the $max_pages parameter to the get_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.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php
// 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( 'cat=1&paged=' . $paged ); 
?>

<?php if ( $the_query->have_posts() ) : ?>

<?php
// the loop
while ( $the_query->have_posts() ) : $the_query->the_post(); 
?>
<?php the_title(); ?>
<?php endwhile; ?>

<?php

// get_next_posts_link() usage with max_num_pages
echo get_next_posts_link( 'Older Entries', $the_query->max_num_pages );
echo get_previous_posts_link( 'Newer Entries' );
?>

<?php 
// clean up after our query
wp_reset_postdata(); 
?>

<?php else:  ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

get_next_posts_link() 示例

历史

添加于 版本 2.7.0

get_next_posts_link() 历史

源文件

get_next_posts_link() 函数的代码位于 wp-includes/link-template.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Return the next posts page link.
 *
 * @since 2.7.0
 *
 * @global int      $paged
 * @global WP_Query $wp_query
 *
 * @param string $label    Content for link text.
 * @param int    $max_page Optional. Max pages.
 * @return string|void HTML-formatted next posts page link.
 */
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;

	$nextpage = intval($paged) + 1;

	if ( null === $label )
		$label = __( 'Next Page »' );

	if ( !is_single() && ( $nextpage <= $max_page="" )="" )="" {="" *="" *="" filter="" 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="">" . preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&$1', $label) . '';
	}
}

get_next_posts_link() 源文件