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
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:
Then optionally add the current category’s name:
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.Use native php functions
It’s not that hard, if you take a in-depth look at string/array handling on php.net.
Note: Not tested
Though a bit old thread, I have come out with this:
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.