• Resolved aradams

    (@aradams)


    Howdy,

    I would like to add the Project tags to the body_class so I can do fancy stuff πŸ˜‰

    I am trying to work with this snippet, and adapt it to the Project Tags but I am not having any luck:

    function add_category_name($classes = '') {
       if(is_single()) {
          $category = get_the_category();
          $classes[] = 'category-'.$category[0]->slug;
       }
       return $classes;
    }
    add_filter('body_class','add_category_name');

    Any help would be greatly appreciated.

    https://wordpress.org/plugins/jetpack/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter aradams

    (@aradams)

    I think I figured it out. I added this to my theme’s body_classes function:

    function mytheme_body_classes( $classes ) {
    
    	// Add the Jetpack Portfolio Tag
      	if ( is_single() ) {
    		global $post;
    		$taxonomy = 'jetpack-portfolio-tag';
    		$tax_terms = get_terms( $taxonomy );
    			foreach ( $tax_terms as $tax_term ) {
    	  		$classes[] = 'jetpack-portfolio-tag-' . $tax_term->slug;
    	  		}
    		}
    
    	return $classes;
    }
    add_filter( 'body_class', 'mytheme_body_classes' );

    Hope this helps someone else!

    Thread Starter aradams

    (@aradams)

    Hmm, now that I have used this it seems that it is grabbing all the terms, not just the one that is attached to this project. Any ideas how to fix this?

    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic πŸš€

    You almost had it. This should do the trick:

    function mytheme_body_classes( $classes ) {
    
    	// Only for single Portfolio pages
    	if ( is_singular( 'jetpack-portfolio' ) ) {
    		global $post;
    		$taxonomy = 'jetpack-portfolio-tag';
    		// get_terms was too generic, you need to use get_the_terms to be able to pass the post ID.
    		$tax_terms = get_the_terms( $post->ID, $taxonomy );
    		if ( $tax_terms ) {
    			foreach ( $tax_terms as $tax_term ) {
    				$classes[] = 'jetpack-portfolio-tag-' . $tax_term->slug;
    			}
    		}
    	}
    
    	return $classes;
    }
    add_filter( 'body_class', 'mytheme_body_classes' );
    Thread Starter aradams

    (@aradams)

    Thank you Jeremy, that worked perfectly!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to add Project Tags to body class?’ is closed to new replies.