wp_link_pages()
云策文档标注
概述
wp_link_pages() 函数用于在分页文章中显示页面链接列表,必须在 The Loop 内使用。它支持自定义输出格式,包括链接文本、HTML 包装和分页样式。
关键要点
- 函数用于输出分页文章的页面链接,如使用 Quicktag 的文章。
- 必须在 The Loop 内调用,否则可能无法正常工作。
- 参数 $args 为可选,可以是数组或字符串,用于自定义输出样式,如 before、after、link_before、link_after、next_or_number 等。
- 默认输出为数字分页,但可通过 next_or_number 参数切换为“上一页/下一页”模式。
- 支持多个过滤器钩子,如 wp_link_pages_args、wp_link_pages 和 wp_link_pages_link,允许开发者修改参数和输出。
- 返回 HTML 字符串,根据 echo 参数决定是否直接输出。
代码示例
$args = array(
'before' => '<p>' . __( 'More pages: ', 'textdomain' ) . '</p>',
'after' => '</p>',
'link_before' => '<span>',
'link_after' => '</span>',
'next_or_number' => 'next',
'separator' => ' | ',
'nextpagelink' => __( 'Next »', 'textdomain' ),
'previouspagelink' => __( '« Previous', 'textdomain' ),
);
wp_link_pages( $args );注意事项
- 确保在 The Loop 内调用此函数,否则可能无法正确获取分页数据。
- 参数 aria_current 用于设置 aria-current 属性值,增强可访问性,默认值为 'page'。
- 使用过滤器钩子时,注意参数类型和返回值,以避免破坏默认功能。
原文内容
The formatted output of a list of pages.
Description
Displays page links for paginated posts (i.e. including the <!--nextpage--> Quicktag one or more times). This tag must be within The Loop.
Parameters
$argsstring|arrayoptional-
Array or string of default arguments.
beforestringHTML or text to prepend to each link. Default is<p> Pages:.afterstringHTML or text to append to each link. Default is</p>.link_beforestringHTML or text to prepend to each link, inside the<a>tag.
Also prepended to the current item, which is not linked. Default empty.link_afterstringHTML or text to append to each Pages link inside the<a>tag.
Also appended to the current item, which is not linked. Default empty.aria_currentstringThe value for the aria-current attribute. Possible values are'page','step','location','date','time','true','false'. Default is'page'.next_or_numberstringIndicates whether page numbers should be used. Valid values are number and next. Default is'number'.separatorstringText between pagination links. Default is ‘ ‘.nextpagelinkstringLink text for the next page link, if available. Default is ‘Next Page’.previouspagelinkstringLink text for the previous page link, if available. Default is ‘Previous Page’.pagelinkstringFormat string for page numbers. The % in the parameter string will be replaced with the page number, so ‘Page %’ generates “Page 1”, “Page 2”, etc.
Defaults to'%', just the page number.echoint|boolWhether to echo or not. Accepts1|trueor0|false. Default1|true.
Source
function wp_link_pages( $args = '' ) {
global $page, $numpages, $multipage, $more;
$defaults = array(
'before' => '<p class="post-nav-links">' . __( 'Pages:' ),
'after' => '</p>',
'link_before' => '',
'link_after' => '',
'aria_current' => 'page',
'next_or_number' => 'number',
'separator' => ' ',
'nextpagelink' => __( 'Next page' ),
'previouspagelink' => __( 'Previous page' ),
'pagelink' => '%',
'echo' => 1,
);
$parsed_args = wp_parse_args( $args, $defaults );
/**
* Filters the arguments used in retrieving page links for paginated posts.
*
* @since 3.0.0
*
* @param array $parsed_args An array of page link arguments. See wp_link_pages()
* for information on accepted arguments.
*/
$parsed_args = apply_filters( 'wp_link_pages_args', $parsed_args );
$output = '';
if ( $multipage ) {
if ( 'number' === $parsed_args['next_or_number'] ) {
$output .= $parsed_args['before'];
for ( $i = 1; $i <= $numpages; $i++ ) {
$link = $parsed_args['link_before'] . str_replace( '%', $i, $parsed_args['pagelink'] ) . $parsed_args['link_after'];
if ( $i !== $page || ! $more && 1 === $page ) {
$link = _wp_link_page( $i ) . $link . '</a>';
} elseif ( $i === $page ) {
$link = '<span class="post-page-numbers current" aria-current="' . esc_attr( $parsed_args['aria_current'] ) . '">' . $link . '</span>';
}
/**
* Filters the HTML output of individual page number links.
*
* @since 3.6.0
*
* @param string $link The page number HTML output.
* @param int $i Page number for paginated posts' page links.
*/
$link = apply_filters( 'wp_link_pages_link', $link, $i );
// Use the custom links separator beginning with the second link.
$output .= ( 1 === $i ) ? ' ' : $parsed_args['separator'];
$output .= $link;
}
$output .= $parsed_args['after'];
} elseif ( $more ) {
$output .= $parsed_args['before'];
$prev = $page - 1;
if ( $prev > 0 ) {
$link = _wp_link_page( $prev ) . $parsed_args['link_before'] . $parsed_args['previouspagelink'] . $parsed_args['link_after'] . '</a>';
/** This filter is documented in wp-includes/post-template.php */
$output .= apply_filters( 'wp_link_pages_link', $link, $prev );
}
$next = $page + 1;
if ( $next <= $numpages ) {
if ( $prev ) {
$output .= $parsed_args['separator'];
}
$link = _wp_link_page( $next ) . $parsed_args['link_before'] . $parsed_args['nextpagelink'] . $parsed_args['link_after'] . '</a>';
/** This filter is documented in wp-includes/post-template.php */
$output .= apply_filters( 'wp_link_pages_link', $link, $next );
}
$output .= $parsed_args['after'];
}
}
/**
* Filters the HTML output of page links for paginated posts.
*
* @since 3.6.0
*
* @param string $output HTML output of paginated posts' page links.
* @param array|string $args An array or query string of arguments. See wp_link_pages()
* for information on accepted arguments.
*/
$html = apply_filters( 'wp_link_pages', $output, $args );
if ( $parsed_args['echo'] ) {
echo $html;
}
return $html;
}
Hooks
- apply_filters( ‘wp_link_pages’, string $output, array|string $args )
-
Filters the HTML output of page links for paginated posts.
- apply_filters( ‘wp_link_pages_args’, array $parsed_args )
-
Filters the arguments used in retrieving page links for paginated posts.
- apply_filters( ‘wp_link_pages_link’, string $link, int $i )
-
Filters the HTML output of individual page number links.
Skip to note 9 content
hearvox
Use previous/next option (instead of page numbers); customize text, HTML, and classes:
'<div class="page-links-XXX"><span class="page-link-text">' . __( 'More pages: ', 'textdomain' ) . '</span>', 'after' => '</div>', 'link_before' => '<span class="page-link">', 'link_after' => '</span>', 'next_or_number' => 'next', 'separator' => ' | ', 'nextpagelink' => __( 'Next »', 'textdomain' ), 'previouspagelink' => __( '« Previous', 'textdomain' ), ); wp_link_pages( $args ); ?>The above displays on the page as this:
More pages: « Previous | Next »
(“Previous” and “Next” links will not print if on the first or last pages, respectively.)
Skip to note 10 content
hearvox
Example:
If the content of a page (or post) has at least one
<!--nextpage-->tag (and this code is in The Loop), this prints linked page numbers (“Pages: 1 2 3”), without a link on current page number, and by default within<p>tags:Skip to note 11 content
hearvox
Display page-links within other HTML tags:
Prints page links as list items within an unordered list, and with custom class names:
&after;=</ul>&link;_before=<li class="page-link">&link;_after=</li>' ); ?>Skip to note 12 content
Steven Lin
Migrated from Codex:
Default Usage
Displays page-links by default with paragraph tags before and after, using Next page and Previous page, listing them with page numbers as Page 1, Page 2 and so on.
Skip to note 13 content
Steven Lin
Migrated from Codex:
Page-links in Paragraph Tags
&after;=</p>&next;_or_number=number&pagelink;=page %'); ?>Skip to note 14 content
Steven Lin
Migrated from Codex:
Page-links in DIV
Displays page-links in DIV for CSS reference as
.&after;=</div>'); ?>