Home › Forums › Calendar Products › Events Calendar PRO › tribe category nicename as CSS class on body tag
- This topic has 8 replies, 2 voices, and was last updated 6 years, 9 months ago by Heinz Duschanek.
-
AuthorPosts
-
July 12, 2017 at 3:23 am #1319925Heinz DuschanekParticipant
Hi,
due to Layout/Design reasons we need the tribe-event cateory nicename as a body class. We find that already in a <div> within the page, but how do we get that as a body class? The usual code does not work for these posts:
add_filter(‘body_class’,’add_category_to_single’);
function add_category_to_single($classes, $class) {
if (is_single() ) {
global $post;
foreach((get_the_category($post->ID)) as $category) {
// add category slug to the $classes array
$classes[] = $category->category_nicename;
}
}
// return the $classes array
return $classes;
}July 12, 2017 at 3:37 am #1319933Heinz DuschanekParticipantI mean this code:
function category_id_class($classes) { global $post; foreach((get_the_category($post->ID)) as $category) $classes[] = $category->category_nicename; return $classes; } add_filter('body_class', 'category_id_class');
July 12, 2017 at 6:18 am #1319967Heinz DuschanekParticipantI figured out this here, which at least adds the tribe category to the body CSS in case of single events:
add_filter('body_class','category_id_class'); function category_id_class($classes) { if(in_the_loop()) { $post_id = get_the_ID(); } else { $post_id = get_queried_object_id(); } $eventCatSlug = ''; $terms = wp_get_post_terms($post_id, 'tribe_events_cat'); $count = count($terms); if ( $count > 0 ){ foreach ( $terms as $term ) { $eventCatSlug = $eventCatSlug . ' tribe-events-category-' . $term->slug; } } $classes[] = $eventCatSlug; return $classes; }
Uhm, tribe team, are you serious by not providing something easier to get the category slug in functions.php???
How would I get the same result for a category of events instead of the single event? It is just not sufficient to have it later in a <div>…
July 13, 2017 at 6:19 am #1320489ShelbyParticipantHey Hedu,
I’m happy to help out as much as possible here.
I apologize that you’re not satisfied with our current code structure. That said, if you have custom needs to alter it, the best plan of action is to hire someone to help. We have some guidlines for making changes yourself here, and we have recommendations for finding freelancers here, but we don’t support changes to our code on these forums.
It’s also worth noting that we happily offer refunds if you’re not satisfied with the product, and you can request one via this form here.
Please let me know if I’ve misunderstood anything or if you have any further questions or need clarification about anything.
Best,
Shelby 🙂
July 13, 2017 at 6:33 am #1320497Heinz DuschanekParticipantHi Shelby,
thank you for your answer, but I think this is a misunderstanding.
First: I do not need external PHP freelancer help, I can tweak code by myself.
Second: I am just asking (as so many others here also did) how to get the slug of a tribe category within a function in functions.php. This is not an unsolvable mystery, I guess, so why not provide a working code snippet to me and the others?The themers guide the support team refers to is wonderful, but no help here.
The category slug is a already there, as I can see it within <body> in a <div>-tag. All I ask is for some lines of code to put it into the <body>-tag additionally, as we need it for styling purposes.
July 13, 2017 at 7:52 am #1320550Heinz DuschanekParticipantFor other users searching here: another solution to get the category slug into the body CSS classes for a single event (to be used in functions.php):
add_filter('body_class','conf_class'); function conf_class($classes) { if(in_the_loop()) { $post_id = get_the_ID(); } else { $post_id = get_queried_object_id(); } $terms = tribe_get_event_cat_slugs($post_id); foreach ($terms as $term) { // loop needed? $eventCatSlug = $eventCatSlug . 'tribe_events-category-' . $term; } $classes[] = $eventCatSlug; return $classes; }
Maybe the foreach loop is not needed, as it also works with:
$classes[] = 'tribe_events-category-' . $terms[0];
Now I try to figure out the same thing for category archive pages. Wish me luck.
July 17, 2017 at 6:50 pm #1322412ShelbyParticipantHi,
I’m glad you seem to have gotten on the right trail here, and we appreciate you posting your findings for other users!
Thanks for understanding about customizations, so sorry that the resources we have for custom code aren’t super helpful in your case!
Best,
Shelby
August 8, 2017 at 9:35 am #1332264Support DroidKeymasterHey there! This thread has been pretty quiet for the last three weeks, so we’re going to go ahead and close it to avoid confusion with other topics. If you’re still looking for help with this, please do open a new thread, reference this one and we’d be more than happy to continue the conversation over there.
Thanks so much!
The Events Calendar Support Team -
AuthorPosts
- The topic ‘tribe category nicename as CSS class on body tag’ is closed to new replies.