WordPress函数get_next_posts_link获取下一页链接函数

WordPress函数get_next_posts_link()获取下一页链接函数

WordPress函数get_next_posts_link()获取下一页链接函数

  在 WordPress CMS内容管理系统中,get_next_posts_link()是一个内置函数,用于获取指向当前查询中上一组帖子的链接。因为 post 查询通常按时间倒序排序,通常指向较旧的条目(靠近集合的末尾),通常指向较新的条目(靠近集合的开头)get_next_posts_link(),get_previous_posts_link()。

  推荐:什么是Trackback?WordPress怎么禁用Trackback

get_next_posts_link()函数基本语法

echo get_next_posts_link();

描述

  检索下一篇文章页面链接。

用法

<?php get_next_posts_link( $label, $max_page ); ?>

参数

  $label

  ( string ) ( 可选) 链接文本的内容。默认:''

  $max_page

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

  推荐:如何禁用右键单击WordPress图像

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

	$next_page = (int) $paged + 1;

	if ( null === $label ) {
		$label = __( 'Next Page &raquo;' );
	}

	if ( ! is_single() && ( $next_page <= $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 sprintf(
			'<a href="%1$s" %2$s>%3$s</a>',
			next_posts( $max_page, false ),
			$attr,
			preg_replace( '/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $label )
		);
	}
}

  推荐:NEX-Forms插件下载WordPress表单生成器插件+ Addons

如何使用get_next_posts_link()

  自定义标签,使用自定义标签检索下一个帖子页面链接。

echo get_next_posts_link( 'Go to the next page' );

  自定义标签和最大页数,检索带有自定义标签和最大页数的下一篇文章页面链接。

echo get_next_posts_link( 'Go to the next page', 4 );

  与 WP_Query 一起使用,使用 WP_Query 查询循环时检索下一篇文章页面链接。将$max_page参数传递给get_next_posts_link()函数。

$paged = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1;
$the_query = new WP_Query( 'cat=1&paged=' . $paged );

if ( $the_query->have_posts() ) :
    while ( $the_query->have_posts() ) : $the_query->the_post();
        the_title();
    endwhile;

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

    wp_reset_postdata();
else :
    echo '<p>No posts matched your criteria.</p>';
endif;

  自定义帖子类型的用法,检索自定义帖子类型的下一个帖子页面链接。

$paged = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1;
$the_query = new WP_Query( array(
    'post_type' => 'my_custom_post_type',
    'paged'     => $paged
) );

if ( $the_query->have_posts() ) :
    while ( $the_query->have_posts() ) : $the_query->the_post();
        the_title();
    endwhile;

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

    wp_reset_postdata();
else :
    echo '<p>No posts matched your criteria.</p>';
endif;

  推荐:[最新版]Asset CleanUp Pro插件免费下载WordPress性能优化插件

  推荐:WordPress函数使用手册

给文章评分

Claude、Netflix、Midjourney、Chatgpt Plus账号购买,ChatGPT API购买,优惠码XDBK,用户购买的时候输入优惠码可以打95折

Chatgpt-Plus注册购买共享账号
滚动至顶部