WordPress函数wp_link_pages()生成文章页面分页链接

描述:

生成文章页面分页链接

用法:

<?php
$defaults = array(
    'before' => '<p>' . __('Pages:') ,
    'after' => '</p>',
    'link_before' => '',
    'link_after' => '',
    'next_or_number' => 'number',
    'separator' => ' ',
    'nextpagelink' => __('Next page') ,
    'previouspagelink' => __('Previous page') ,
    'pagelink' => '%',
    'echo' => 1
);
wp_link_pages($defaults); ?>

参数:

before

(string) 文本放在所有链接之前。默认为<p>页:。

after

(string) 文本放在所有链接之后。默认为</p>。

link_before

(string) 链接文本之前的文本。默认值2(空白)。需要2.7版或更高版本。

link_after

(string) 链接文本后面的文本。默认值2(空白)。需要2.7版或更高版本。

next_or_number

(string) 指示是否应使用页码。有效值为:

  • number (数字分页 默认)
  • next (下一页)

separator

(string) 页码之间使用的文本(如果适用)。默认为单个可断开空间。(在WordPress 3.6或更高版本中有效)

nextpagelink

(string) 链接到下一页的文本。默认为下一页。(在WordPress 1.5或之后有效)

previouspagelink

(string) 链接到上一页的文本。默认为上一页。(在WordPress 1.5或之后有效)

pagelink

(string) 数字分页格式,字符串中的.%将替换为数字,因此页%将生成“第1页”、“第2页”等。默认为%。(boolean):

  • 1 (True) – 默认
  • 0 (False)

示例:

<?php wp_link_pages(); ?>

源文件:

/**
 * The formatted output of a list of pages.
 *
 * Displays page links for paginated posts (i.e. includes the .
 * Quicktag one or more times). This tag must be within The Loop.
 *
 * @since 1.2.0
 *
 * @global int $page
 * @global int $numpages
 * @global int $multipage
 * @global int $more
 *
 * @param string|array $args {
 *     Optional. Array or string of default arguments.
 *
 *     @type string       $before           HTML or text to prepend to each link. Default is `
 Pages:`.
 *     @type string       $after            HTML or text to append to each link. Default is `

`.
 *     @type string       $link_before      HTML or text to prepend to each link, inside the `` tag.
 *                                          Also prepended to the current item, which is not linked. Default empty.
 *     @type string       $link_after       HTML or text to append to each Pages link inside the `` tag.
 *                                          Also appended to the current item, which is not linked. Default empty.
 *     @type string       $next_or_number   Indicates whether page numbers should be used. Valid values are number
 *                                          and next. Default is 'number'.
 *     @type string       $separator        Text between pagination links. Default is ' '.
 *     @type string       $nextpagelink     Link text for the next page link, if available. Default is 'Next Page'.
 *     @type string       $previouspagelink Link text for the previous page link, if available. Default is 'Previous Page'.
 *     @type string       $pagelink         Format 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.
 *     @type int|bool     $echo             Whether to echo or not. Accepts 1|true or 0|false. Default 1|true.
 * }
 * @return string Formatted output in HTML.
 */
function wp_link_pages( $args = '' ) {
	global $page, $numpages, $multipage, $more;

	$defaults = array(
		'before'           => '
' . __( 'Pages:' ),
		'after'            => '

',
		'link_before'      => '',
		'link_after'       => '',
		'next_or_number'   => 'number',
		'separator'        => ' ',
		'nextpagelink'     => __( 'Next page' ),
		'previouspagelink' => __( 'Previous page' ),
		'pagelink'         => '%',
		'echo'             => 1
	);

	$params = wp_parse_args( $args, $defaults );

	/**
	 * Filter the arguments used in retrieving page links for paginated posts.
	 *
	 * @since 3.0.0
	 *
	 * @param array $params An array of arguments for page links for paginated posts.
	 */
	$r = apply_filters( 'wp_link_pages_args', $params );

	$output = '';
	if ( $multipage ) {
		if ( 'number' == $r['next_or_number'] ) {
			$output .= $r['before'];
			for ( $i = 1; $i <= $numpages;="" $i++="" )="" {="" $link="$r['link_before']" .="" str_replace(="" '%',="" $i,="" $r['pagelink']="" )="" .="" $r['link_after'];="" if="" (="" $i="" !="$page" ||="" !="" $more="" &&="" 1="=" $page="" )="" {="" $link="_wp_link_page(" $i="" )="" .="" $link="" .="">';
				}
				/**
				 * Filter 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 ) ? ' ' : $r['separator'];
				$output .= $link;
			}
			$output .= $r['after'];
		} elseif ( $more ) {
			$output .= $r['before'];
			$prev = $page - 1;
			if ( $prev ) {
				$link = _wp_link_page( $prev ) . $r['link_before'] . $r['previouspagelink'] . $r['link_after'] . '';

				/** 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="" .="$r['separator'];" }="" $link="_wp_link_page(" $next="" )="" .="" $r['link_before']="" .="" $r['nextpagelink']="" .="" $r['link_after']="" .="">';

				/** This filter is documented in wp-includes/post-template.php */
				$output .= apply_filters( 'wp_link_pages_link', $link, $next );
			}
			$output .= $r['after'];
		}
	}

	/**
	 * Filter 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  $args   An array of arguments.
	 */
	$html = apply_filters( 'wp_link_pages', $output, $args );

	if ( $r['echo'] ) {
		echo $html;
	}
	return $html;
}