Community Forums › Forums › Archived Forums › General Discussion › How to set the preferred category in breadcrumbs??
- This topic has 5 replies, 5 voices, and was last updated 7 years, 7 months ago by
sangfroidweb.
-
AuthorPosts
-
December 23, 2012 at 11:35 am #7177
Shawn Teo
Memberhi, How to set the preferred category in breadcrumbs when a post is assigned different categories?
thanks for advice and merry christmas
December 23, 2012 at 12:14 pm #7182Susan
ModeratorJanuary 7, 2013 at 2:08 pm #10165Jason
MemberI would also like to do something similar - but not set a single referred category, set a 'dynamic' category. For example, if a user clicks on "Category C" they might see posts / pages that are in only Category C, but those posts may also exist in other categories.
If they click through to a specific post, I would like the breadcrumb to display the category that the user selected, not a default or 'primary' category. Is this possible?
Thanks!
Jason Larsen. Owner of Qoala Group.
January 7, 2013 at 10:53 pm #10274wpsmith
MemberYou need to install the sCategory Permalink plugin. Please let me know if it doesn't work. However, there is built-in support in the Breadcrumbs function/class for you to accomplish this with this plugin.
October 27, 2015 at 1:14 pm #169236sangfroidweb
ParticipantHi!,
Susan points to the breadcrumb snippets page for this, but that page doesn't address the original poster's issue, which is my issue now. I guess this post is a few years old, so the snippet page may have had a solution previously, but unless I'm missing something, I don't see any mention of a way to set a preferred category.... or am I missing something?
Liz or Eddy @SangFroid Web — Customizing StudioPress themes at http://www.wpstudioworks.com and building custom Genesis Child themes for businesses at http://www.sangfroidwebdesign.com 🙂
October 27, 2015 at 2:32 pm #169244sangfroidweb
ParticipantIn case this helps anyone else in the future.... I did some digging into the breadcrumb class and found a solution in the get_post_crumb function of the breadcrumb class. When the heirarchial_categories arg is set to true (default) the developers included support for a plugin: sCategory Permalink plugin (mentioned by moderator wpsmith, above) however that plugin hasn't been updated in 6 years, so I'm avoiding like the plague. The developers still have the support in the class though, so we can use that. Basically it checks the post meta for the meta_key "_category_permalink", which from the code I can tell contains the ID of a preferred category. Now that I knew that, I had 2 choices.. go through and create a meta value for each and every post... *yuk* or find a way to filter get_post_meta the return value of the get_post_meta function, which the get_post_crumb function uses to find out if that meta_value is set. Luckily WordPress has a filter we can use, get_post_metadata. So now if anyone comes knocking, looking for that meta_key in any post.. we can return what we want. Here's the function I put together:
// Filter breadcrumb category preference
add_filter('get_post_metadata', 'preferred_category', true, 4);
function preferred_category($metadata, $object_id, $meta_key, $single){global $post;
// is a value for our target meta_key being sought?
if($meta_key == '_category_permalink'){// If the current post is checked with our preferred category, return that categoy's ID as the metadata
if( in_category( 5 , $post ) ){
$metadata = 5;
}}
return $metadata;
}
You just need to set the category ID to your preferred category id ( 5 for me in the example above ).
It seems to be working. The only caveat is, if the developers decide to dump support for that meta_key, since the original plugin which was the impetus for it's use seems to have been orphaned... this code will no longer work. So dear developers.. if you notice this post. Don't discontinue support for _category_permalink. Thanks!
I hope this helps anyone else looking for a default breadcrumb category!
Liz or Eddy @SangFroid Web — Customizing StudioPress themes at http://www.wpstudioworks.com and building custom Genesis Child themes for businesses at http://www.sangfroidwebdesign.com 🙂
-
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.