Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Breadrcumbs: Get current category/slug path only for post WordPress

  • SOLVED

Hi,

I'm having a slight problem with breadcrumbs.

When a post is assigned to two or more categories, my breadcrumbs then display all the categories associated with the post instead of just the current category path as exhibited in the URL.

Example,

"My Post Title" is assigned "Category A" and "Category B"

I am viewing "My Post Title" in "Category B" (www.example.com/category-b/my-post-title)

The breadcrumbs show:

You are in: Category A >>
Category B >> My Post Title

Instead of,

You are in: Category B >> My Post Title


So to recap,

I need to show breadcrumbs that are relative to the path of the URL including any categories/sub-categories instead of all categories associated with the "My Post Title".

As you can see showing all categories/sub-categories associated with a post in breadcrumbs can be confusing for visitors.

Thank you,
Wprrrrring

Answers (2)

2011-06-13

Fahad Murtaza answers:

Please share your category breadcrumb code. And do you have any preference to which category you want to show in the breadcrumb navigation?


Wordpressing comments:

Hi Fahd,

I want to show the current breadcrumb path so that it matches the slug/permalink structure in your browser address bar.

So if a post has multiple categories it needs to only show the current path relative to where the user is viewing the post.

The function I am using in my functions.php file is:



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


I've tried several other custom functions written by other WP developers but all produce the same result.

Thank you


Fahad Murtaza comments:

Got it, I am working on it.

I have just one thing for you to take note of.

On a category page, you can get the name and ID of the current category from path. But I am not sure how to know once category name for a unique category if you are on a single page.

What is your permalink structure as it can help.


Fahad Murtaza comments:

My bad,

Please ignore my last question.

I am viewing "My Post Title" in "Category B" (www.example.com/category-b/my-post-titl

I got it.


Fahad Murtaza comments:

This should work

Line # 92 of your code


#blog-title {font-size:64px;line-height:60px;letter-spacing:-2px;width:585px;display:inline;float:left; display:none; visibility:hidden;}


Fahad Murtaza comments:

sorry, my last response was not for you.


Fahad Murtaza comments:

Enjoy your new breadcrumbs :)

You can remove the comments.




// breadcrumbs

function the_breadcrumb() {

if (!is_home()) {

echo '<a href="';

echo get_option('home');

echo '">';

bloginfo('name');

echo "</a> » ";

if (is_category() || is_single()) {

// the_category('title_li=');
//print_r(get_the_category());
$_SERVER['REQUEST_URI_PATH'] = preg_replace('/\\?.*/', '', $_SERVER['REQUEST_URI']);
// echo "<br/>server=".$_SERVER['REQUEST_URI_PATH'];
$url_segments= split('/',$_SERVER['REQUEST_URI_PATH']);
$total = count($url_segments);
// echo "<br/>count= ". $total;
if(is_single())
{
$category_slug = $url_segments[$total-3];
}
else if(is_category())
{
$category_slug = $url_segments[$total-2];
}


// echo "<br/>Category slug = ". $category_slug;
$cat_info= get_category_by_slug( $category_slug );

// echo "<pre>";
// print_r($cat_info);
// echo "</pre>";

// Type cast for easy handling

$cat_info = (array) $cat_info;
$cat_name = $cat_info['name'];


//echo "<br/>Category name = ". $cat_name;
echo $cat_name;
if (is_single()) {

echo " » ";

the_title();

}

} elseif (is_page()) {

echo the_title();

}

}

}


Fahad Murtaza comments:

Let me know if you tried that code.


Fahad Murtaza comments:

Let me know if you still need help. Did my solution work as I want to mature it based on your feedback.

2011-06-13

Denzel Chia answers:

Hi,

Have you tried this plugin?

http://wordpress.org/extend/plugins/breadcrumb-trail/

Thanks.
Denzel