Results 1 to 8 of 8

Thread: Custom Taxonomy as CSS Class

  1. #1
    curtismchale is offline Hello World
    Join Date
    Dec 2009
    Location
    BC, Canada
    Posts
    38

    Default Custom Taxonomy as CSS Class

    I need to style each of my custom taxonomies a bit different so I thought I'd add a CSS class to the wrapper and then i can target them. I've looked at get_terms, get_term.....for about 5 hours and am still not getting any closer.

    Point me in the right direction?

  2. #2
    kucrut is offline Hello World
    Join Date
    Apr 2010
    Posts
    11

    Default body class

    You could extend the
    PHP Code:
    body_class() 
    function using its
    Code:
    body_class
    filter :)

  3. #3
    mfields's Avatar
    mfields is offline Here For The Peanuts
    Join Date
    Nov 2009
    Location
    Portland, OR
    Posts
    168

    Default

    Crazy, I actually have this code 'just laying around' :)

    PHP Code:
    add_filter'body_class''mysite_body_class'10 );
    if( !
    function_exists'mysite_body_class' ) ) {
        
    /**
         * Body Class.
         * @since 2010-06-22
         */
        
    function mysite_body_class$classes ) {
            if( 
    is_tax() ) {
                global 
    $taxonomy;
                if( !
    in_array$taxonomy$classes ) )
                    
    $classes[] = $taxonomy;
            }
            return 
    $classes;
        }


  4. #4
    curtismchale is offline Hello World
    Join Date
    Dec 2009
    Location
    BC, Canada
    Posts
    38

    Default

    awesome just in time for a long flight with lots of coding planned. I don't think this quite fits my use case (though I probably didn't explain it well). I'll need to apply the class inside the loop against a number of posts so each instance of the post will need to have a different class.

    post 1

    Code:
    <p class="the-special-class">the special taxonomy</p>
    post 2

    Code:
    <p class="the-other-special-class">the other special taxonomy</p>
    Last edited by curtismchale; 07-10-2010 at 06:37 AM. Reason: more description

  5. #5
    Otto's Avatar
    Otto is offline On The Rocks
    Join Date
    Apr 2009
    Location
    Memphis, TN
    Posts
    865

    Default

    Do similar to the above, but with post_class instead.

    But I guess I'm not understanding how you're using custom taxonomies here. A taxonomy is like tags, you have a taxonomy with a name which applies to all posts. It has terms in it which connect directly to posts. So, what exactly is it that you're wanting to show? Those terms? Or do you want those terms to apply to the post as CSS classes? Or what?

  6. #6
    curtismchale is offline Hello World
    Join Date
    Dec 2009
    Location
    BC, Canada
    Posts
    38

    Default

    So I'll try to explain my use case a bit better. It's a custom post type of Music shows with a custom taxonomy of the music genre. Each genre has the same basic display styling but the colour needs to be different for each genre. Assuming we're inside the loop we'd look at code like this.

    Code:
    <h3>Post One Title</h3>
    
    <p class="country">Country</p>
    
    <h3>Post Two Title</h3>
    
    <p class="hip-hop">Hip Hop</p>
    I figured i could extract the taxonomy slug and echo that into the class but haven't been able to figure that out.

  7. #7
    mfields's Avatar
    mfields is offline Here For The Peanuts
    Join Date
    Nov 2009
    Location
    Portland, OR
    Posts
    168

    Default

    The following will work, just make sure that your theme is calling post_class() on the main wrapper for your post.
    PHP Code:
    add_filter'post_class''mysite_post_class'10);
    if( !
    function_exists'mysite_post_class' ) ) {
        
    /**
         * Append taxonomy terms to post class.
         * @since 2010-07-10
         */
        
    function mysite_post_class$classes$class$ID ) {
            
    $taxonomy 'status';
            
    $terms get_the_terms( (int) $ID$taxonomy );
            if( !empty( 
    $terms ) ) {
                foreach( (array) 
    $terms as $order => $term ) {
                    if( !
    in_array$term->slug$classes ) ) {
                        
    $classes[] = $term->slug;
                    }
                }
            }
            return 
    $classes;
        }


  8. #8
    curtismchale is offline Hello World
    Join Date
    Dec 2009
    Location
    BC, Canada
    Posts
    38

    Default

    yeah i thought about it more and extending post class is awesome. Actually caught your post on it when I came over on my cell. Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •