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 added breadcrumb navigation to WordPress & I'm facing one problem. Here's the function.php code of the breadcrumb:

function ux_breadcrumbs() {
    if (!is_home()) {
        echo '<a href="';
        echo get_option('home');
        echo '">';
        bloginfo('name');
        echo "</a> » ";
        if (is_category() || is_single()) {
            the_category('/');
            echo " » ";
            if (is_single()) {
                echo "  ";
                the_title();
            }
        } elseif (is_page()) {
            echo the_title();
        }
    }
}

The above code is displaying all categories of the post. I just want it to display only one category. Thanks in advance!

UPDATE: Thanks for the help...here's one more thing:

I want to know how to display category > sub category in the breadcrumb if exists.

share|improve this question
    
For posts that have multiple (or hierarchical) categories, how will you determine which category is the correct one to display? –  Chip Bennett Jan 13 at 18:26
add comment

1 Answer

up vote 1 down vote accepted

get_the_category() function used to retrieve categories array of a post, and array_shift() function used to get the first item of an array.

You possibly need this -

function ux_breadcrumbs() {
    if (!is_home()) {
        echo '<a href="';
        echo get_option('home');
        echo '">';
        bloginfo('name');
        echo "</a> » ";
        if (is_category() || is_single() )
        {
            if( is_category() )
            {
                single_term_title();
            }
            elseif (is_single() )
            {
                echo " » ";
                $cats = get_the_category( get_the_ID() );
                $cat = array_shift($cats);
                echo '<a href="' . esc_url( get_category_link( $cat->term_id ) ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $cat->name ) ) . '">'. $cat->name .'</a>';
                echo "  ";
                the_title();
            }
        } elseif (is_page()) {
            echo the_title();
        }
    }
}
share|improve this answer
    
Thanks for the help..!!! One more thing! Do you know how to show "Category > Sub Category" if exist in this above code? –  Hardeep Asrani Jan 15 at 2:02
    
Yes ofcourse, change the question or post a new one and i will answer. thanks. –  Shazzad Jan 15 at 22:00
    
I updated the question. Please help me now. Thanks! –  Hardeep Asrani Jan 16 at 19:27
add comment

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.