tribe category nicename as CSS class on body tag

Home Forums Calendar Products Events Calendar PRO tribe category nicename as CSS class on body tag

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #1319925
    Heinz Duschanek
    Participant

    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;
    }

    #1319933
    Heinz Duschanek
    Participant

    I 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');
    #1319967
    Heinz Duschanek
    Participant

    I 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>…

    #1320489
    Shelby
    Participant

    Hey 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 🙂

    #1320497
    Heinz Duschanek
    Participant

    Hi 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.

    #1320550
    Heinz Duschanek
    Participant

    For 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.

    #1322412
    Shelby
    Participant

    Hi,

    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

    #1332264
    Support Droid
    Keymaster

    Hey 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

Viewing 8 posts - 1 through 8 (of 8 total)
  • The topic ‘tribe category nicename as CSS class on body tag’ is closed to new replies.