Breadcrumbs showing Parent and Child Pages

We are trying to create a function for breadcrumbs that will show not only the path to the current page, but also to other pages with the same parent page.

For instance, on example.com/parent1/child3, the breadcrumbs would show Home > Parent1 > Child1 > Child2 > Child3 > Child4 rather than simply Home > Parent1 > Child3.

We are using a modified version of the breadcrumbs script from 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 . "https://wordpress.stackexchange.com/" . $slug['slug'] . "https://wordpress.stackexchange.com/">' . $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 .= '&nbsp;' . __('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

Any ideas? We really appreciate it.

1 Answer
1

idk about working it back into your breadcrumbs… which by nature i think are hierarchical.

but this should give you a list of the pages that have the same parent as the page you are on (aka its siblings) albeit in a totally unstyled way

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;

if you list your current page and then list off its siblings then you can programmatically recreate what you are looking for i think

edit 1: setup postdata so we can use get_permalink(), get_the_title() inside a “loop” etc instead of object methods. get_permalink() gets the URL to the current item in the loop

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;

the args are different, but
http://codex.wordpress.org/Function_Reference/get_posts#Reset_after_Postlists_with_offset
is the foreach loop i usually work off of. it is also got get_posts, but get_pages works the same except that it only returns pages.

Leave a Comment