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

目录

描述

Displays page-links for paginated posts (i.e. includes the <!--nextpage--> Quicktag one or more times). This works in much the same way as link_pages() (deprecated), the difference being that arguments are given in query string format. This tag must be within The_Loop.

wp_link_pages() 描述

用法

 <?php wp_link_pages$args ); ?> 

Default Usage

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?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 );?>

wp_link_pages() 用法

参数

before 

(string) Text to put before all the links. Defaults to <p>Pages:.

after 

(string) Text to put after all the links. Defaults to </p>.

link_before 

(string) Text that goes before the text of the link. Defaults to (blank). Version 2.7 or later required.

link_after 

(string) Text that goes after the text of the link. Defaults to (blank). Version 2.7 or later required.

next_or_number 

(string) Indicates whether page numbers should be used. Valid values are:

  • number (Default)
  • next (Valid in WordPress 1.5 or after)

separator 

(string) Text to be used between page numbers (if applicable). Defaults to a single breakable space. (Valid in WordPress 3.6 or after)

nextpagelink 

(string) Text for link to next page. Defaults to Next page. (Valid in WordPress 1.5 or after)

previouspagelink

(string) Text for link to previous page. Defaults to Previous page. (Valid in WordPress 1.5 or after)

pagelink 

(string) Format string for page numbers.  % in the string will be replaced with the number, so Page % would generate "Page 1", "Page 2", etc. Defaults to %.

echo 

(boolean) Toggles whether to echo or return the result. The default is true. Valid values:

  • 1 (True) - Default
  • 0 (False)

wp_link_pages() 参数

返回值

(mixed) 

If $echo is set to true (default), returns NULL and echos the content. If $echo is set to false, returns formatted output in HTML.

wp_link_pages() 返回值

示例

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.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php wp_link_pages(); ?>

Page-links in Paragraph Tags

Displays page-links wrapped in paragraph tags.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php wp_link_pages('before=<p>&after=</p>&next_or_number=number&pagelink=page %'); ?>

Page-links in DIV

Displays page-links in DIV for CSS reference as <div id="page-links">.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php wp_link_pages('before=<div id="page-links">&after=</div>'); ?>

Adding wp_link_pages in content.php

This code snippet can be added directly to your content.php or single.php file in the position you want your pagination to display.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

<?php wp_link_pages( array(
	'before'      => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentyfourteen' ) . '</span>',
	'after'       => '</div>',
	'link_before' => '<span>',
	'link_after'  => '</span>',
	) );
?>

wp_link_pages() 示例

源文件

wp_link_pages() 函数的代码位于 wp-includes/post-template.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * 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; }

wp_link_pages() 源文件

相关