Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 . '/' . $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 .= '&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.

share|improve this question
    
when i drop this into my theme (after i comment out the dt_first_letter_function() that i dont have)... i get parent > child > grandchild > you are here. what is the problem exactly? –  helgatheviking Mar 7 '12 at 14:48
    
I'd like it to show other pages of the same level. So instead of just showing the path/where you have been, it would show other child pages with the same parent. If Parent1 has 4 Child Pages, it would show all 4 child pages with CSS styling to highlight the current (child) page. In other words, it would show Home > Parent1 > Child1 > Child2 > Child3 (Current Page) > Child4. An example of this using categories can be found here: link. Do you think there's any way to do this without having to create a custom menu on every page? –  Nick Mar 7 '12 at 15:13
    
nick, sorry i didn't understand what you wanted as i assumed child1 and child implied a hierarchy of parent > child > grandchild instead of a sibling relationship. i've posted a solution that should get you the sibling pages for any particular page –  helgatheviking Mar 7 '12 at 15:45

1 Answer 1

up vote 0 down vote accepted

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.

share|improve this answer
    
Thank you so much, helga! That worked perfectly to get the other pages to show up... how would I get them to link? Sorry, I'm a newbie with PHP. I really appreciate your help! –  Nick Mar 7 '12 at 16:25
    
THANK YOU!!! It has moved the sibling pages to the same line, but for some reason they still are not linking to their respective pages... –  Nick Mar 8 '12 at 0:06
    
"sibling pages to the same line" ? i made them output in an unordered list so you can style them however you wish. the links work fine for me. i just tested again. have you changed anything? this isn't really meant to be pasted into the breadcrumbs function, so it probably could... i just didn't test it that way. –  helgatheviking Mar 8 '12 at 0:48
    
Yes, sorry. I needed to adjust something in CSS. Now it works great. Thanks for all your help! I really appreciate it! –  Nick Mar 8 '12 at 11:01
    
you're welcome. if my answer solved your question could you mark it as the asnwer? (the green checkbox) –  helgatheviking Mar 8 '12 at 14:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.