返回介绍

wp_link_pages()

发布于 2017-09-11 12:21:29 字数 7852 浏览 853 评论 0 收藏 0

wp_link_pages( string|array $args = '' )

The formatted output of a list of pages.


description

Displays page links for paginated posts (i.e. includes the . Quicktag one or more times). This tag must be within The Loop.


参数

$args

(string|array) (Optional) Array or string of default arguments.

  • 'before'
    (string) HTML or text to prepend to each link. Default is <p> Pages:.
  • 'after'
    (string) HTML or text to append to each link. Default is </p>.
  • 'link_before'
    (string) HTML or text to prepend to each link, inside the <a> tag. Also prepended to the current item, which is not linked.
  • 'link_after'
    (string) HTML or text to append to each Pages link inside the <a> tag. Also appended to the current item, which is not linked.
  • 'next_or_number'
    (string) Indicates whether page numbers should be used. Valid values are number and next. Default is 'number'.
  • 'separator'
    (string) Text between pagination links. Default is ' '.
  • 'nextpagelink'
    (string) Link text for the next page link, if available. Default is 'Next Page'.
  • 'previouspagelink'
    (string) Link text for the previous page link, if available. Default is 'Previous Page'.
  • 'pagelink'
    (string) 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.
  • 'echo'
    (int|bool) Whether to echo or not. Accepts 1|true or 0|false. Default 1|true.

Default value: ''


返回值

(string) Formatted output in HTML.


源代码

File: wp-includes/post-template.php

function wp_link_pages( $args = '' ) {
	global $page, $numpages, $multipage, $more;

	$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
	);

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

	/**
	 * Filters 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 . '</a>';
				}
				/**
				 * 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 ) ? ' ' : $r['separator'];
				$output .= $link;
			}
			$output .= $r['after'];
		} elseif ( $more ) {
			$output .= $r['before'];
			$prev = $page - 1;
			if ( $prev > 0 ) {
				$link = _wp_link_page( $prev ) . $r['link_before'] . $r['previouspagelink'] . $r['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 .= $r['separator'];
				}
				$link = _wp_link_page( $next ) . $r['link_before'] . $r['nextpagelink'] . $r['link_after'] . '</a>';

				/** This filter is documented in wp-includes/post-template.php */
				$output .= apply_filters( 'wp_link_pages_link', $link, $next );
			}
			$output .= $r['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  $args   An array of arguments.
	 */
	$html = apply_filters( 'wp_link_pages', $output, $args );

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

更新日志

Versiondescription
1.2.0Introduced.

相关函数

Uses

  • wp-includes/l10n.php: __()
  • wp-includes/functions.php: wp_parse_args()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/post-template.php: _wp_link_page()
  • wp-includes/post-template.php: wp_link_pages_args
  • wp-includes/post-template.php: wp_link_pages_link
  • wp-includes/post-template.php: wp_link_pages
  • Show 2 more uses Hide more uses

Used By

  • wp-includes/deprecated.php: link_pages()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 1You must log in to vote on the helpfulness of this note Contributed by 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:

    <?php wp_link_pages(); ?>
  2. Display page-links within other HTML tags:
    Prints page links as list items within an unordered list, and with custom class names:

    <?php wp_link_pages( 'before=<ul class="page-links">&after=</ul>&link_before=<li class="page-link">&link_after=</li>' ); ?>

    Use previous/next option (instead of page numbers); customize text, HTML, and classes:

    
    <?php
    $args = array (
    	'before'      		=> '<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 &raquo', 'textdomain' ),
    	'previouspagelink'	=> __( '&laquo 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.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文