1

Just a question : I want to use get_category_parents() to display a breadcrumb on a category archive page, but with no link on the current displayed category (SEO purposes, because they say that's a link to itself. I'm not sure search engines are that stupid, but anyway).

Like this :

link_home » link_cat1 » link_subcat1 » nolink_subsubcat1

get_category_parents() is perfect for that, but there's only two options : with links and without links.

What I want is links BUT on the last item.

The function is returning a string, not a object or array, so I cannot remove the last item.

I could do with regex by searching with the » separator and remove last link that way, but i'm pretty bad with regexes (if you know good references for that, i'm interested !).

I know the obvious answer is to create a custom function using get_ancestors() and a loop , then simply adding after the current category name.

But I wanted to know is there is some more simplier way, but just hooking get_category_parents() to not adding link to the last item ?

Thank you for any insight.

Regards Simon

  • Please take your time and format Qs properly. – kaiser Feb 16 '12 at 14:19
  • Sorry Kaiser, what's Qs ? – Simon Feb 16 '12 at 14:35
  • 1
    Qs means Questions :D – Stephen Harris Feb 16 '12 at 14:38
  • 1
    Ok sorry, I didn't get the ` ` and > formating caracters. Will remember it for next time. Thank you. – Simon Feb 16 '12 at 14:45
3

I wouldn't consider this any better/worse than Kaiser's option, but just different. Why not use get_category_parents on the parent category and then (optionally) append the current category?

I haven't tested this, but something like the following should work:

$cat_id=7;//current category's ID (e.g. 7)
$separator='»';//The separator to use
$category = get_category($cat_id);//$category is the current category object
$parent_id = $category[0]->category_parent //category's parent ID
$ancestors = get_category_parents($parent_id, true, $separator);

Then optionally add the current category's name:

 if($ancestors){
      $breadcrumb = $ancestors.$separator.' <span class="active-cat">'.single_cat_title().'</span>';
 }else{
      $breadcrumb = '<span class="active-cat">'.single_cat_title().'</span>';
 }
     echo $breadcrumb;

EDIT:

It turns out this almost exactly how WordPress produces the output: get_category_parents calls itself recursively (see here), so with this method you are essentially 'stopping it early', and manually completing it. There are no hooks that can achieve this effect however.

  • Working, with some minor changes (displaying the separator when get_category_parents returns a WP_error and using get_category instead of get_the_category). I could provide code in answer or editing question. Thank you very much. I appreciate the logical answer, should've thought of it... – Simon Feb 16 '12 at 15:44
3

Use native php functions

It's not that hard, if you take a in-depth look at string/array handling on php.net.

// 1. Calls the category parents including their links
// 2. Explodes the string to an array with limit -1 to avoid outputting the last element
// 3. Loops through the array and echos the breadcrumbs
// 3.a Shows the » only after the first breadcrumb
foreach( explode( '//', get_category_parents( $cat, true, '//' ), -1 ) as $index => $breadcrumb )
    echo $index <= 0 ? $breadcrumb : " &raquo; {$breadcrumb}";
// 4. Echo the current category with a leading »
echo ' &raquo; <span class="breadcrumb-active-cat">'.single_cat_title().'</span>';

Note: Not tested

  • +1 Was just doing something very similar. You should change get_the_category() though - that's the category object rather than name...? – Stephen Harris Feb 16 '12 at 14:22
  • @StephenHarris As always: If you think you got something that makes the A better - feel free to edit. – kaiser Feb 16 '12 at 14:25
  • Working, except not displaying sub categories but only current category. I'm not too familiar with this syntax so I'll go with Stephen answer, if you don't mind. But thank you, that was very close of what I have in mind. – Simon Feb 16 '12 at 15:41
  • @kaiser and stephen I loved both of your answer and noticed something and posted an answer, I would like to know if there is any problem in the way I have done it . – Prafulla Kumar Sahu Sep 29 '16 at 10:00
  • 1
    @PrafullaKumarSahu - there's nothing wrong with it. In fact, it looks like it's the same as my answer, except you've assumed that the current item is returned is returned by get_queried_object() - which is probably a correct assumption. You've omitted the current item from the breadcrumb - the OP wanted it there, just without a link. – Stephen Harris Sep 29 '16 at 19:51
0

In a category page you can also do and it will leave current category from the breadcrumb and all its parent categories will be displayed.

   echo get_category_parents( get_queried_object()->parent, true, ' &rarr; ' ); 
0

Though a bit old thread, I have come out with this:

$catpars= get_category_parents($cat_id, true, ' &raquo; ');
$catpars= preg_replace('/\W\w+\s*(\W*)$/', '', $catpars);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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