描述:
显示带有链接的页面列表
用法:
<?php
$args = array(
'depth' => 0,
'sort_column' => 'menu_order, post_title',
'menu_class' => 'menu',
'include' => '',
'exclude' => '',
'echo' => true,
'show_home' => false,
'link_before' => '',
'link_after' => ''
); ?>
参数:
depth
(integer) 此参数控制页面层次结构中要包含在wp_list_pages生成的列表中的级别。默认值为0(显示所有页面,包括所有子页面)。
- 0 (默认)以任意层级显示页面,并在嵌套列表中按层次排列页面
- -1 以任意层级显示页面,并将其排列在单个平面列表中
- 1 仅显示顶级页面
- 2, 3 … 按给定层级显示页面
sort_column
(string) 按页面标题按字母顺序排列页面列表。默认设置是按菜单顺序排序和按页面标题字母顺序排序。sort_column参数可用于根据WordPress数据库wp_post表中任何字段的描述符对页面列表进行排序。这里列出了一些有用的例子。
- 'post_title' - 按标题按字母顺序对页面排序。
- 'menu_order' - 按页面顺序对页面排序。请注意页面顺序和页面ID之间的区别。页面ID是WordPress分配给每个帖子或页面的唯一编号。页面顺序可以由用户在管理面板中设置(例如,管理>页面>编辑)。
- 'post_date' - 按创建时间排序。
- 'post_modified' - 按最后修改的时间排序。
- 'ID' - 按页面ID排序。
- 'post_author' - 按页面作者的ID排序。
- 'post_name' - 按页面标题排序
menu_class
(string) 显示列表的div类。默认为菜单。
include
(string) 仅列出用相应id标识的页面,即wp_page_menu('include=2,14')将只列出id为2和14的页面
exclude
(string) 定义要从列表中排除的页ID的逗号分隔列表(例如:“exclude=3,7,31”)。没有默认值。请参见下面的“从列表中排除页面”示例。
exclude_tree
(string) 定义要排除的父页面ID以逗号分隔列表。使用此参数可排除父页和该父页的所有子页。因此'exclude_tree=5'将排除父页5及其子(所有子)页。(此参数是在版本2.7中添加的,它的功能与版本2.8.1中描述的不同-请参阅错误报告。)
echo
(boolean) 切换生成的链接列表的显示,或将该列表作为要在PHP中使用的HTML文本字符串返回。默认值为1(显示生成的列表项)。有效值:
- 0 (不显示)
- 1 (显示) - 默认
show_home
(boolean) 在“页面”列表中添加“主页”作为第一项。分配给“主页”的URL从“管理>设置>常规”中的博客地址(URL)中提取。默认值为0(在生成的列表中不显示“Home”)。有效值:
- 0 (不显示) - 默认
- 1 (显示)
- <any text> - 使用此文本作为“Home”的链接(show_Home仍然被认为是真的)
link_before
(string) 设置要在<a>标签中的链接文本之前添加文本或html。
link_after
(string) 设置要在<a>标签中的链接文本之后添加文本或html。
示例:
下面的示例将“Home”添加到所显示页面列表的开头。此外,包装在div元素(页面id 5、9和23)中的页面将从显示的页面列表中排除,并按页面顺序列出页面。列表的开头是“页面菜单”,
<h2>Page Menu</h2>
<?php wp_page_menu('show_home=1&exclude=5,9,23&menu_class=page-navi&sort_column=menu_order'); ?>
将标题为“Blog”的页面作为主页显示
以下示例将“Blog”(而不是“Home”)添加到显示的页面列表的开头:
<?php wp_page_menu( array( 'show_home' => 'Blog', 'sort_column' => 'menu_order' ) ); ?>
仅显示主页
以下示例仅显示指向“主页”的链接。请注意,包含“= 9999”引用不存在的页ID,因此仅显示家中的链接。
<?php wp_page_menu('show_home=1&include=9999'); ?>
源文件:
/**
* Display or retrieve list of pages with optional home link.
*
* The arguments are listed below and part of the arguments are for {@link
* wp_list_pages()} function. Check that function for more info on those
* arguments.
*
* @since 2.7.0
*
* @param array|string $args {
* Optional. Arguments to generate a page menu. See wp_list_pages() for additional arguments.
*
* @type string $sort_column How to short the list of pages. Accepts post column names.
* Default 'menu_order, post_title'.
* @type string $menu_class Class to use for the div ID containing the page list. Default 'menu'.
* @type bool $echo Whether to echo the list or return it. Accepts true (echo) or false (return).
* Default true.
* @type string $link_before The HTML or text to prepend to $show_home text. Default empty.
* @type string $link_after The HTML or text to append to $show_home text. Default empty.
* @type int|bool|string $show_home Whether to display the link to the home page. Can just enter the text
* you'd like shown for the home link. 1|true defaults to 'Home'.
* }
* @return string|void HTML menu
*/
function wp_page_menu( $args = array() ) {
$defaults = array('sort_column' => 'menu_order, post_title', 'menu_class' => 'menu', 'echo' => true, 'link_before' => '', 'link_after' => '');
$args = wp_parse_args( $args, $defaults );
/**
* Filter the arguments used to generate a page-based menu.
*
* @since 2.7.0
*
* @see wp_page_menu()
*
* @param array $args An array of page menu arguments.
*/
$args = apply_filters( 'wp_page_menu_args', $args );
$menu = '';
$list_args = $args;
// Show Home in the menu
if ( ! empty($args['show_home']) ) {
if ( true === $args['show_home'] || '1' === $args['show_home'] || 1 === $args['show_home'] )
$text = __('Home');
else
$text = $args['show_home'];
$class = '';
if ( is_front_page() && !is_paged() )
$class = 'class="current_page_item"';
$menu .= '
' . $args['link_before'] . $text . $args['link_after'] . '
';
// If the front page is a page, add it to the exclude list
if (get_option('show_on_front') == 'page') {
if ( !empty( $list_args['exclude'] ) ) {
$list_args['exclude'] .= ',';
} else {
$list_args['exclude'] = '';
}
$list_args['exclude'] .= get_option('page_on_front');
}
}
$list_args['echo'] = false;
$list_args['title_li'] = '';
$menu .= str_replace( array( "
", "
", " " ), '', wp_list_pages($list_args) );
if ( $menu )
$menu = '
' . $menu . '
';
$menu = '
' . $menu . "
";
/**
* Filter the HTML output of a page-based menu.
*
* @since 2.7.0
*
* @see wp_page_menu()
*
* @param string $menu The HTML output.
* @param array $args An array of arguments.
*/
$menu = apply_filters( 'wp_page_menu', $menu, $args );
if ( $args['echo'] )
echo $menu;
else
return $menu;
}
//
// Page helpers
//