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.

I have pieced together a breadcrumbs code that displays child pages/posts and is markedup for schema.

The only thing this code doesn't have is a separator between each link. How can I add a separator in one instance, rather than adding the separator after each individual generated link?

function crumbs() {
 if ((is_page() && ! is_category()) || is_archive() || is_single() || is_single()) {
 echo '<div class="crumbs"><span xmlns:v="http://rdf.data-vocabulary.org/#">';
 echo '<span typeof="v:Breadcrumb"><a rel="v:url" property="v:title" href="'.get_bloginfo('url').'">Home</a></span>';
 $post_ancestors = get_post_ancestors($post);
 if ($post_ancestors) {
 $post_ancestors = array_reverse($post_ancestors);
 foreach ($post_ancestors as $crumb)
 echo '<span typeof="v:Breadcrumb"><a rel="v:url" property="v:title" href="'.get_permalink($crumb).'">'.get_the_title($crumb).'</a></span>';
 }
 if (is_category() || is_single()) {
 $category = get_the_category();
 echo '<span typeof="v:Breadcrumb"><a rel="v:url" property="v:title" href="'.get_category_link($category[0]->cat_ID).'">'.$category[0]->cat_name.'</a></span>';
 }
 if (!is_category())
 echo '<span typeof="v:Breadcrumb"><span class="crumbs-last" property="v:title">'.get_the_title().'</span></span>';
 echo '</span></div>';
 }
}
share|improve this question

2 Answers 2

up vote 1 down vote accepted

I'm not sure if you want it like this, however, this can be achieved by CSS.

.crumbs > span > span + span {
    margin-left: 1em;
}

.crumbs > span > span + span:before {
    content: '|'; /* or something else */
    *display: inline;
    display: inline-block;
    margin-left: -1em;
    width: 1em;
    text-align: center;
}

// EDIT
If you want it PHP-only, then this is a way to go:

function crumbs() {
    if ((is_page() && ! is_category()) || is_archive() || is_single() || is_single()) {
        $crumbs = '';
        $crumbs .= '<div class="crumbs"><span xmlns:v="http://rdf.data-vocabulary.org/#">';
        $crumbs .= '<span typeof="v:Breadcrumb"><a rel="v:url" property="v:title" href="'.get_bloginfo('url').'">Home</a></span>';
        $post_ancestors = get_post_ancestors($post);

        if ($post_ancestors) {
            $post_ancestors = array_reverse($post_ancestors);
            foreach ($post_ancestors as $crumb)
                $crumbs .= '<span typeof="v:Breadcrumb"><a rel="v:url" property="v:title" href="'.get_permalink($crumb).'">'.get_the_title($crumb).'</a></span>';
        }

        if (is_category() || is_single()) {
            $category = get_the_category();
            $crumbs .= '<span typeof="v:Breadcrumb"><a rel="v:url" property="v:title" href="'.get_category_link($category[0]->cat_ID).'">'.$category[0]->cat_name.'</a></span>';
        }

        if (!is_category())
            $crumbs .= '<span typeof="v:Breadcrumb"><span class="crumbs-last" property="v:title">'.get_the_title().'</span></span>';

        $crumbs .= '</span></div>';

        $close_crumb = '</span>';
        $open_crumb = '<span typeof="v:Breadcrumb">';
        $separator = '<span class="separator">|</span>';
        echo str_replace($close_crumb.$open_crumb, $close_crumb.$separator.$open_crumb, $crumbs);
    }
}

Yes, it's not nice - but that's due to the whole thing, IMHO.

share|improve this answer
    
@t f Thanks. I am aware of the ability to do this with CSS, but I am looking for a PHP solution. However, this is helpful to those who may be searching for a breadcrumb divider on Google. –  AndrettiMilas Feb 7 at 15:48

If your theme includes a filter for breadcrumbs, you can use code like this in your functions file however theme specific questions are generally off topic here.

add_filter('theme_breadcrumb_args_filter', 'customize_separator_breadcrumbs');

function customize_separator_breadcrumbs($args) {
$args['sep'] = ' >> ';
return $args;
}

Replace theme_breadcrumb_args_filter with your themes breadcrumbs filter.

share|improve this answer
    
Strange, it isn't working for whatever reason. –  AndrettiMilas Feb 7 at 23:57
    
It doesn't work unless your parent theme includes a filter for breadcrumbs. If your theme includes a filter for breadcrumbs, you can use code like this in your functions file however theme specific questions are generally off topic here. –  Brad Dalton Feb 8 at 9:09

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.