我使用一个函数在我的页面中创建面包屑,它可以工作:
function the_breadcrumb() {
$currentBefore = \'<li><a>\';
$currentAfter = \'</a></li>\';
if ( !is_home() && !is_front_page() || is_paged() ) {
echo \'<nav class="breadcrumb"><ul>\';
global $post;
if ( is_page() && !$post->post_parent ) {
echo $currentBefore;
the_title();
echo $currentAfter; }
elseif ( is_page() && $post->post_parent ) {
$parent_id = $post->post_parent;
$breadcrumbs = array();
while ($parent_id) {
$page = get_page($parent_id);
$breadcrumbs[] = \'<li><a href="\' . get_permalink($page->ID) . \'">\' . get_the_title($page->ID) . \'</a></li>\';
$parent_id = $page->post_parent;
}
$breadcrumbs = array_reverse($breadcrumbs);
foreach ($breadcrumbs as $crumb) echo $crumb;
echo $currentBefore;
the_title();
echo $currentAfter;
}
echo \'</ul></nav>\';
}
}
但我希望此函数将post\\u id(页面的id)作为参数,以便在为该页面创建面包屑的AJAX函数中使用它:
function the_ajax_breadcrumb($post_id) {
ob_start();
$args = array(\'page_id\' => $post_id);
$the_query = new WP_Query($args);
$currentBefore = \'<li><a>\';
$currentAfter = \'</a></li>\';
while ( $the_query->have_posts() ) {
$the_query->the_post();
if ( !is_home() && !is_front_page() || is_paged() ) {
echo \'<nav class="breadcrumb"><ul>\';
if ( is_page() && !$the_query->post->post_parent ) {
echo $currentBefore;
the_title();
echo $currentAfter;
}
elseif ( is_page() && $the_query->post->post_parent ) {
$parent_id = $the_query->post->post_parent;
$breadcrumbs = array();
while ($parent_id) {
$page = get_page($parent_id);
$breadcrumbs[] = \'<li><a href="\' . get_permalink($page->ID) . \'">\' . get_the_title($page->ID) . \'</a></li>\';
$parent_id = $page->post_parent;
}
$breadcrumbs = array_reverse($breadcrumbs);
foreach ($breadcrumbs as $crumb) echo $crumb;
echo $currentBefore;
the_title();
echo $currentAfter;
}
else {
echo \'<li>nope</li>\';
}
echo \'</ul></nav>\';
}
}
$result = ob_get_clean();
return $result;
}
但它输出“nope”(else条件)。。。我在这里错过了什么让这一切成功?非常感谢您的时间和帮助。