WordPress:Template Tags/wp list pages
描述[ ]
模板标签,wp_list_pages(),显示了WordPres网页的一个列表,作为一列链接。wp_list_pages()通过用来自定义Sidebar或者 Header,但是也可能在其它的模板中使用。
WordPress1.5版本以及更新的版本中拥有这个模板标签。
用法[ ]
%%% <?php wp_list_pages('arguments'); ?> %%%
例子[ ]
默认用法[ ]
$defaults = array( 'depth' => 0, 'show_date' => '', 'date_format' => get_option('date_format'), 'child_of' => 0, 'exclude' => '', 'title_li' => __('Pages'), 'echo' => 1, 'authors' => '', 'sort_column' => 'menu_order, post_title');
默认情况下,用法显示:
- 显示了所有的网页和子网页(没有层级限制)
- 不显示创建的日期
- 不限制到任何网页的子网页
- 不删除任何网页
- 网页的标题列为"网页"
- 结果得到反映(显示)
- 文章不限制为某个作者的
- 有网页顺序分类然后有网页标题分类。
- 以升序分类(不在上面的默认设置中显示)
- 网页以层级嵌套的方式显示(不在上面的默认设置中显示)
- 包含所有的网页(不在上面的默认设置中显示)
- 不限制为拥有特别的meta key/meta参数值的网页(不显示在上述的默认设置中)
wp_list_pages();
隐藏或者更改列表标头[ ]
wp_list_pages产生的网页("网页")列表的默认标题,可以通过向title_li 参数传递一个零或者空参数值,得到隐藏。下面的例子显示了列表上方没有标题文本。
<ul>
<?php
wp_list_pages('title_li='); ?>
</ul>
在下面的例子中,只有ID是9,5,23的网页包含在列表中而且标题文本被更改为"诗",标头形式为<h2>:
<ul>
<?php
wp_list_pages('include=5,9,23&title_li=<h2>' . __('Poetry') . '</h2>' ); ?>
</ul>
根据网页顺序列出网页[ ]
下面的例子,根据写 > 网页 管理面板中设置的每个网页的排序来列出网页。
<ul>
<?php
wp_list_pages('sort_column=menu_order'); ?>
</ul>
如果你想要根据网页顺序给列表分类,而且在工具条上显示"散文"作为列表标题(以h2样式),你可以添加以下的代码到sidebar.php文件上:
<ul>
<?php
wp_list_pages('sort_column=menu_order&title_li=<h2>' . __('Prose') . '</h2>' ); ?>
</ul>
使用下面的这些代码,网页会以网页顺序显示,而且还没有标题:
<ul>
<?php
wp_list_pages('sort_column=menu_order&title_li='); ?>
</ul>
根据文章日期给网页分类[ ]
这个例子显示了网页根据(创建)日期分类,而且在每个网页列表item的旁边显示了日期。
<ul>
<?php
wp_list_pages('sort_column=post_date&show_date=created'); ?>
</ul>
从列表中删除网页[ ]
使用删除参数隐藏wp_list_pages产生的列表中的某个网页。
<ul>
<?php
wp_list_pages('exclude=17,38' ); ?>
</ul>
在列表中包含网页[ ]
在一个列表中只包含某个特别的网页,例如,ID数字为35,7,26和13的网页,使用包含参数。
<ul>
<?php
wp_list_pages('include=7,13,26,35&title_li=<h2>' . __('Pages') . '</h2>' ); ?>
</ul>
列出子网页(Wordpress 2.0.1之前的版本)[ ]
将这个放到the_content()后的WordPress主题的page.php 模板的the_post()部分的内部,或者将这个放到page.php模板的一个拷贝中,你为拥有子网页的网页使用这个page.php模板:
<ul>
<?php
global $id;
// teensy bit insane, sorry
$date_format = '<\b\r /><\s\p\a\n '.
'\s\t\y\l\e="\f\o\n\t-\s\i\z\e: \s\m\a\l\l\e\r">'.
'\l\a\s\t\m\o\d Y-n-j</\s\p\a\n>';
wp_list_pages("title_li=&child_of=$id&show_date=modified
&date_format=$date_format"); ?>
</ul>
注:如果这个例子放到了一个网页模板中,在WordPress2.0.1或者更新的版本中不能够运行,因为没有设置全局$id。使用下面的代码
列出子网页 (Wordpress 2.0.1 或者更新的版本)[ ]
注意:即使没有子网页,也需要一个HTML标签(<ul> 或者<ol>)。当你使用css设计列表的时候,要牢记这一点。
<ul>
<?php
wp_list_pages('title_li=&child_of='.$post->ID.'&show_date=modified
&date_format=$date_format'); ?>
</ul>
只有当前的网页拥有子网页的时候,下面的例子才会产生一个列表(子网页是指定当前网页为母网页的网页):
<?php
$children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
if ($children) { ?>
<ul>
<?php echo $children; ?>
</ul>
<?php } ?>
即使没有子网页,也列出子网页[ ]
上述的例子指示显示了来自于母网页的子网页,但是并不是真正地在一个子网页上,这个代码会显示子网页,而且只有子网页在母网页上或者在其中的一个子网页上时,才会显示。
- 警醒 ** 如果将这个代码放到一个工具条上的一个widget 块上时,这个代码就不会运行。
<?php
if($post->post_parent)
$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
else
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
if ($children) { ?>
<ul>
<?php echo $children; ?>
</ul>
<?php } ?>
网页items的标记和样式[ ]
默认情况下,wp_list_pages()产生一个无序的,嵌套的WordPress网页,这个网页是用写 > 网页管理面板创建的。你可以通过将title_li参数设置为一个空字符串,可以移除最外面的item(li.pagenav)并且列出(ul)。
wp_list_pages()产生的所有的列表items(li)都标记了class page_item。当显示网页的时候,调用了wp_list_pages(),关于那个网页的列表item会得到额外的classcurrent_page_item。
<li class="pagenav"> Pages <ul> <li class="page_item current_page_parent"> [parent of the current page] <ul> <li class="page_item current_page_item"> [the current page] </li> </ul> </li> <li class="page_item"> [another page] </li> </ul> </li>
这些可以用CSS 选择器设计:
.pagenav { ... } .page_item { ... } .current_page_item { ... } .current_page_parent { ... }
参数[ ]
- sort_column (string)
- 以许多不同的方式给列出的网页分类。默认设置为根据网页标题的字母顺序分类。
- 'post_title' - (根据标题)将网页按照字母顺序分类-默认
- 'post_date' - 根据创建时间分类。
- 'post_modified' - 根据最后一次更改分类。
- 'ID' - 根据网页ID分类。
- 'post_author' -根据网页作者的 ID分类。
- 'post_name' - 根据文章的slug,按字母顺序分类。
注: sort_column参数可以用来给列出的网页分类,根据WordPress数据库的wp_post table的任何栏中的描述符来给文章分类。这里列出了一些有用的例子。
- sort_order (string)
- 更改列出的网页的分类顺序(升序的或者降序的)。默认为升序的,有效的参数值:
- 'asc' -从最低到最高分类 (默认)。.
- 'desc' - 从最高到最低分类。
- exclude (string)
- 默认用逗号将需要从列表上删除的网页的ID分开(例如:'exclude=3,7,31')。没有默认参数值。请看看下面的例子从列表中删除网页。
- include (string)
- 在wp_list_pages产生的列表上只包含特别的网页。像删除,这个参数拥有一个用逗号分开的网页ID列表。没有默认参数值。请看看在一个列表中包含网页下面的例子。
- depth (integer)
- 这个参数控制了wp_list_pages产生的列表中可以包含多少层级的网页。默认值为0(显示所有的网页,包括子网页)。
- 0 -以层级(嵌套)方式(默认)显示的网页和子网页。
- -1 -子网页中的网页以flat(非嵌套)的方式显示。
- 1 -只显示最高级别的网页
- 2 -值为2(或者更高)规定了显示网页时,可以下降的层次(或者层级)。
- child_of (integer)
- 只显示一个单独网页的子网页;为一个网页使用ID作为参数值。默认为0(显示所有网页)。
- show_date (string)
- 在每个网页的旁边显示创建或者最后更改的日期。默认值为零(不显示日期)。有效的参数值:
- '' -不显示日期(默认)。
- 'modified' - 显示最后更改的日期。
- 'xxx' - 除了modified的任何参数值显示了网页首次创建时的日期(文章_日期)。请看看下面的例子。
- date_format (string):控制了show_date参数设置的网页日期格式(例如:"l, F j, Y")。这个参数默认日期格式为配置在WordPress上的选项。请看看日期和时间格式和php 网站上的日期格式网页
- title_li (string)
- 设置网页列表的标头的文本和样式。默认为'__('Pages')',显示了"网页"(为本地化目的使用__(''))。如果传递了一个零或者空的参数值,(''),不显示任何标题,而且列表也不会外包上<ul>, </ul>标签。请看看关于标头的例子。
- echo (boolean)
- Toggles链接产生的列表的显示或者返回链接作为HTML文本字符串,用于PHP。默认值为1(显示产生的列表item)。有效的参数值:
- 1 (true) - 默认
- 0 (false)
- hierarchical
- (boolean) 在母网页下,以嵌套的方式显示子网页,或者列出网页inline。默认为真的(显示嵌套在母列表item下面的子网页)。有效的参数值:
- 1 (true) - default
- 0 (false)
- meta_key
- (string)只包含拥有这个自定义Field Key的网页(和meta_value field用在一起)。
- meta_value
- (string) 只包含拥有这个自定义Field Key参数值的网页(和meta_key field用在一起)。
相关的[ ]
This article is [[WordPress::Category:Copyedits|marked]] as in need of editing. You can help Codex by editing it.