可以在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 = '<ul>';
foreach ($pages as $pagg) {
if (is_page($pagg->ID)) {
$class = '';
}
$option .= '<li' . $class . '>';
$option .= '<a href="' . get_page_link($pagg->ID) . '" title="' . $pagg->post_title . '">' . $pagg->post_title . '</a>';
$option .= '</li>';
$class = '';
}
$option .= '</ul>';
// 退货清单
return $option;
}
该函数通过调用函数来工作,get_pages()以检索属于该页面的子页面。还使用echo参数,并给其赋予0,以使该函数返回页面数组,而不是仅打印出来。要使用此功能,请创建一些有子页面并选择要打印的页面(在本例中为页面ID 29)。
print getChildPagesList(29);
要打印出连接到当前帖子的页面,请使用以下命令:
echo getChildPagesList($post->ID);