我们正在尝试为面包屑创建一个函数,该函数不仅显示当前页面的路径,还显示具有相同父页面的其他页面的路径。
例如,在示例上。com/parent1/child3,面包屑将显示Home>parent1>Child1>Child2>child3>Child4,而不仅仅是Home>parent1>child3。
我们使用的是来自http://dimox.net/wordpress-breadcrumbs-without-a-plugin/:
function dimox_breadcrumbs( $args = array() ) {
$defaults = array(
\'delimiter\' => \'»\',
\'home\' => \'Home\',
\'before\' => \'<span class="current">\',
\'after\' => \'</span>\',
\'before_wrapper\' => \'<div id="crumbs">\',
\'after_wrapper\' => \'</div>\',
\'before_link\' => \'<li>\',
\'after_link\' => \'</li>\'
);
$opts = wp_parse_args( $args, $defaults );
$delimiter = $opts[\'delimiter\'];
$home = $opts[\'home\']; // text for the \'Home\' link
$showCurrent = 0; // 1 - show current post/page title in breadcrumbs, 0 - don\'t show
$before = $opts[\'before\']; // tag before the current crumb
$after = $opts[\'after\']; // tag after the current crumb
$b_link = $opts[\'before_link\'];
$a_link = $opts[\'after_link\'];
$title = \'\';
$sub_title = \'\';
if ( 1 || (!is_home() && !is_front_page()) || is_paged() ) {
echo $opts[\'before_wrapper\'];
$out = \'<ul>\';
global $post;
$homeLink = get_bloginfo(\'url\');
$out .= $b_link . \'<a href="\' . $homeLink . \'">\' . $home . \'</a>\' . $a_link . $delimiter;
if ( is_category() ) {
global $wp_query;
$cat_obj = $wp_query->get_queried_object();
$thisCat = $cat_obj->term_id;
$thisCat = get_category($thisCat);
$parentCat = get_category($thisCat->parent);
if ($thisCat->parent != 0) {
$out .= $b_link . (get_category_parents($parentCat, TRUE, $delimiter)) . $a_link;
}
$title = __( \'Archive by category "%"\', LANGUAGE_ZONE );
$sub_title = single_cat_title(\'\', false);
$title = str_replace( \'%\', $sub_title, $title );
}elseif ( is_day() ) {
$out .= $b_link . \'<a href="\' . get_year_link(get_the_time(\'Y\')) . \'">\' . get_the_time(\'Y\') . \'</a>\' . $a_link . $delimiter;
$out .= $b_link . \'<a href="\' . get_month_link(get_the_time(\'Y\'),get_the_time(\'m\')) . \'">\' . get_the_time(\'F\') . \'</a>\' . $a_link . $delimiter;
$title = $sub_title = get_the_time(\'d\');
}elseif ( is_month() ) {
$out .= $b_link . \'<a href="\' . get_year_link(get_the_time(\'Y\')) . \'">\' . get_the_time(\'Y\') . \'</a>\' . $a_link . $delimiter;
$title = $sub_title = get_the_time(\'F\');
}elseif ( is_year() ) {
$title = $sub_title = get_the_time(\'Y\');
}elseif ( is_search() ) {
$title = __( \'Search results for "%"\', LANGUAGE_ZONE );
$sub_title = get_search_query();
$title = str_replace( \'%\', $sub_title, $title );
}elseif ( is_single() && !is_attachment() ) {
if ( get_post_type() != \'post\' ) {
$post_type = get_post_type_object(get_post_type());
$slug = $post_type->rewrite;
// echo $b_link . $post_type->labels->singular_name . $a_link . $delimiter;
// echo $b_link . \'<a href="\' . $homeLink . \'/\' . $slug[\'slug\'] . \'/">\' . $post_type->labels->singular_name . \'</a>\' . $a_link . $delimiter;
$title = $sub_title = get_the_title();
}else {
// blog and other stuff
$menu_data = dt_current_menu_item();
// $cat = get_the_category(); $cat = $cat[0];
// echo $b_link . get_category_parents($cat, TRUE, \' \' . $delimiter . \' \') . $a_link;
$out .= $b_link . \'<a href=\' . esc_url($menu_data[\'link\']) . \'>\' . $menu_data[\'title\'] . \'</a>\' . $a_link;
$title = get_the_title();
$sub_title = $menu_data[\'title\'];
}
}elseif ( !is_single() && !is_page() && get_post_type() != \'post\' && !is_404() ) {
$post_type = get_post_type_object(get_post_type());
$title = $sub_title = $post_type->labels->singular_name;
}elseif ( is_attachment() ) {
$parent = get_post($post->post_parent);
// $cat = get_the_category($parent->ID); $cat = $cat[0];
// $out .= get_category_parents($cat, TRUE, \' \' . $delimiter . \' \');
$out .= $b_link . \'<a href="\' . get_permalink($parent) . \'">\' . $parent->post_title . \'</a>\' . $a_link . $delimiter;
$title = $sub_title = get_the_title();
}elseif ( is_page() && !$post->post_parent ) {
$title = $sub_title = get_the_title();
}elseif ( is_page() && $post->post_parent ) {
$parent_id = $post->post_parent;
$breadcrumbs = array();
while ($parent_id) {
$page = get_page($parent_id);
$breadcrumbs[] = $b_link . \'<a href="\' . get_permalink($page->ID) . \'">\' . get_the_title($page->ID) . \'</a>\' . $a_link;
$parent_id = $page->post_parent;
}
$breadcrumbs = array_reverse($breadcrumbs);
foreach ($breadcrumbs as $crumb) $out .= $crumb . $delimiter;
$title = $sub_title = get_the_title();
}elseif ( is_tag() ) {
$title = __( \'Posts tagged "%"\', LANGUAGE_ZONE );
$sub_title = single_tag_title(\'\', false);
$title = str_replace( \'%\', $sub_title, $title );
}elseif ( is_author() ) {
global $author;
$userdata = get_userdata($author);
$title = __( \'Articles posted by %\', LANGUAGE_ZONE );
$sub_title = $userdata->display_name;
$title = str_replace( \'%\', $sub_title, $title );
}elseif ( is_404() ) {
$title = $sub_title = __( \'Error 404\', LANGUAGE_ZONE );
}elseif( is_home() ) {
$title = $sub_title = __( \'Blog\', LANGUAGE_ZONE );
}
if( strlen($title) > 50 ) {
$title = substr( $title, 0, 50 );
$title .= \'...\';
}
$out .= $before . $title . $after;
if ( get_query_var(\'paged\') ) {
if( is_category() ||
is_day() ||
is_month() ||
is_year() ||
is_search() ||
is_tag() ||
is_author() )
{
$out .= \' (\';
}
$out .= \' \' . __(\'Page\', LANGUAGE_ZONE) . \' \' . get_query_var(\'paged\');
if( is_category() ||
is_day() ||
is_month() ||
is_year() ||
is_search() ||
is_tag() ||
is_author() )
{
$out .= \')\';
}
}
$out .= \'</ul>\';
if( $opts[\'show_trail\'] ) {
echo $out;
}
echo \'<h1>\' . dt_first_letter_filter( $sub_title ) . \'</h1>\';
echo $opts[\'after_wrapper\'];
}
} // end dimox_breadcrumbs
有什么想法吗?我们真的很感激。
最合适的回答,由SO网友:helgatheviking 整理而成
我想把它放回你的面包屑里。。。从本质上来说,我认为这是有等级的。
但这应该会给您一个页面列表,其中的父页面与您所在的页面(即其同级页面)具有相同的父页面,尽管完全没有样式
global $post;
$args = array(\'child_of\' => $post->post_parent, \'exclude\'=> $post->ID);
if ($post->post_parent) $pages = get_pages($args);
if ($pages) foreach ($pages as $page):
echo $page->post_title .\'<br/>\';
endforeach;
如果您列出当前页面,然后列出其同级,那么您可以通过编程方式重新创建所需内容
edit 1: 设置postdata,以便我们可以使用get\\u permalink()、在“循环”等中获取\\u title(),而不是对象方法。get\\u permalink()获取循环中当前项的URL
global $post;
$args = array(\'child_of\' => $post->post_parent, \'exclude\'=> $post->ID);
if ($post->post_parent) $pages = get_pages($args);
if ($pages) :
//store the $temp variable
$temp = $post;
$siblings = \'<ul>\';
foreach ($pages as $post): setup_postdata($post);
$siblings .= \'<li><a href="\'.get_permalink().\'">\'.get_the_title().\'</a></li>\';
endforeach;
//restore the $post variable
$post = $temp;
$siblings .= \'</ul>\';
echo $siblings;
endif;
args不同,但是
http://codex.wordpress.org/Function_Reference/get_posts#Reset_after_Postlists_with_offset是我通常使用的foreach循环。它还可以获取get\\u帖子,但get\\u页面的工作原理相同,只是它只返回页面。