我们在阅读完一篇文章后,为了留住用户继续阅读,一般会在文末给出一些相关链接,WordPress函数next_posts_link的作用就是给出当前页面的下一页链接,方便读者继续阅读,有效的降低跳出率,提升用户体验。
next_posts_link官方描述:显示下一篇文章链接。
函数原型
next_posts_link函数位于wp-includes/link-template.php文件中。
复制
function next_posts_link( $label = null, $max_page = 0 ) {
echo get_next_posts_link( $label, $max_page );
}next_posts_link函数是由get_next_posts_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 ) ) {
/**
* 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 '<a href="' . next_posts( $max_page, false ) . "\" $attr>" . preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&$1', $label) . '</a>';
}
}函数参数
$label
字符串值,默认值:null
链接的锚文本,默认输出 下一页 »
$max_pages
整数型,默认值:0
限制最多翻页数,如果设置为2,那么翻到第2页就不再出现“下一页”链接,默认不限制。
next_posts_link()函数使用示例
复制
<?php while ( have_posts() ) : the_post(); the_title(); endif; next_posts_link(); ?>
具体效果见下面的下一篇文章。





评论 (0)