在Wordpress中获取子页面列表
可以在Wordpress中打印出页面列表,但是这些功能旨在从根页面打印到特定级别。我经常需要打印出正在查看的当前页面的子页面列表,因此创建了以下功能。
/**
* Get a list of child pages.
*
* @param integer $id The ID of the parent page.
*
* @return string The list of child pages.
*/
function getChildPagesList($id)
{
//获取当前页面的子页面。
$args = array(
'child_of' => $id,
'echo' => 0,
);
$pages = get_pages($args);
//建立页面列表
$class = '';
$option = '- ';
foreach ($pages as $pagg) {
if (is_page($pagg->ID)) {
$class = '';
}
$option .= '
- '; $option .= '' . $pagg->post_title . ''; $option .= ' '; $class = ''; } $option .= '
该函数通过调用函数来工作,get_pages()以检索属于该页面的子页面。还使用echo参数,并给其赋予0,以使该函数返回页面数组,而不是仅打印出来。要使用此功能,请创建一些有子页面并选择要打印的页面(在本例中为页面ID29)。
printgetChildPagesList(29);
要打印出连接到当前帖子的页面,请使用以下命令:
echogetChildPagesList($post->ID);