对于分层页面面包屑导航,我的做法如下:
// Define Parent Page Hierarchy Crumbs for Child Pages
elseif ( ! is_front_page() && is_page() && $post->post_parent ) {
$parent_id = $post->post_parent;
$breadcrumbs = array();
while ( $parent_id ) {
$page = get_page($parent_id);
$breadcrumbs[] = \'<a href="\' . get_permalink($page->ID) . \'">\' . get_the_title($page->ID) . \'</a>\';
$parent_id = $page->post_parent;
}
$breadcrumbs = array_reverse( $breadcrumbs );
foreach ( $breadcrumbs as $crumb ) {
$hierarchy .= $delimiter . $crumb;
}
$hierarchy = $hierarchy . $delimiter;
// Note: get_the_title() is filtered to output a
// default title if none is specified
$currentLocation = get_the_title();
}
注意,我定义
$delimiter
函数的前面部分。你要找的肉在这里。
首先,获取当前页面父页面的ID:
$parent_id = $post->post_parent;
接下来,设置一个数组以保存分层父页面链接:
$breadcrumbs = array();
接下来,循环遍历每个父页,直到没有其他父页,然后将构造的链接添加到数组中:
// Start looping
while ( $parent_id ) {
// Get the page object
$page = get_page($parent_id);
// Build the HTML link
$breadcrumbs[] = \'<a href="\' . get_permalink($page->ID) . \'">\' . get_the_title($page->ID) . \'</a>\';
// Set the parent ID to the next parent,
// to continue the loop.
// Eventually, this will return 0,
// at which time, the looping will end
$parent_id = $page->post_parent;
}
最后,按正确的顺序放置HTML链接:
$breadcrumbs = array_reverse( $breadcrumbs );
现在,你可以用
$breadcrumbs
大堆