paginate_links()自动生成分页导航

目录

描述

如果你是 WordPress主题开发者,你肯定需要实现存档分页导 功能,虽然目  少方 ,但是倡萌 是推荐尽量使用官方函数来实现。今天介绍的函数 paginate_links,可以用来检 归档 的页面分页链接。其实,该 数可以被 在任何地方实现 页链接。

paginate_links() 描述

用法

<?php echo paginate_links$args ); ?>

Default Arguments

paginate_links() 用法

参数

Even if the whole arguments array is marked optional, the function will output nothing if you do not specify the needed arguments.

base

(string) (可选) Used to reference the url, which will be used to create the paginated links. The default value '%_%' in 'http://example.com/all_posts.php%_%' is replaced by 'format' argument (see below).

默认值: '%_%'

format

(string) (可选) Used for Pagination structure. The default value is '?page=%#%', If using pretty permalinks this would be '/page/%#%', where the '%#%' is replaced by the page number.

默认值: '?page=%#%'

total

(integer) (可选) The total amount of pages.

默认值: 1

current

(integer) (可选) The current page number.

默认值: 0

show_all

(boolean) (可选) If set to True, then it will show all of the pages instead of a short list of the pages near the current page. By default, the 'show_all' is set to false and controlled by the 'end_size' and 'mid_size' arguments.

默认值: False

end_size

(integer) (可选) How many numbers on either the start and the end list edges.

默认值: 1

mid_size

(integer) (可选) How many numbers to either side of current page, but not including current page.

默认值: 2

prev_next

(boolean) (可选) Whether to include the previous and next links in the list or not.

默认值: True

prev_text

(string) (可选) The previous page text. Works only if 'prev_next' argument is set to true.

默认值: __('« Previous')

next_text

(string) (可选) The next page text. Works only if 'prev_next' argument is set to true.

默认值: __('Next »')

type

(string) (可选) Controls format of the returned value. Possible values are:

  • 'plain' - A string with the links separated by a newline character.
  • 'array' - An array of the paginated link list to offer full control of display.
  • 'list' - Unordered HTML list.

默认值: 'plain'

add_args

(array) (可选) An array of query args to add.

默认值: false

add_fragment

(string) (可选) A string to append to each link.

默认值: None

before_page_number

(string) (可选) A string to appear before the page number.

默认值: None

after_page_number

(string) (可选) A string to append after the page number.

默认值: None

paginate_links() 参数

返回值

mixed (array|string

String or array of page links.

paginate_links() 返回值

示例

Basic Example

To add pagination to your search results and archives, you can use the following example:

Example With a Custom Query

When querying a loop with new WP_Query set the 'total' parameter to the max_num_pages property of the WP_Query object.

Example of a custom query:

Example of paginate_links parameters adapted to the custom query above:

Improving Accessibility

To add context to the numbered links to ensure that screen reader users understand what the links are for:

paginate_links() 示例

注意

历史

  • 添加于 版本: 2.1.0

paginate_links() 历史

源文件

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

paginate_links() 源文件