Skip to main content
Tweeted twitter.com/#!/StackWordPress/status/578762865332600832
retagging
Source Link
Johannes Pille
  • 11.1k
  • 3
  • 42
  • 53

I was surprised to learn that custom taxonomies aren't added as body or post classes like categories and tags are.

I'm sure this will be added in a future version of WordPress, but in the meantime I need to add a custom taxonomy to the post class so that I can style post in a certain category in that taxonomy differently.

It'd be most elegant to filter the post class and add the taxonomies to it. [I found a snippet][1] to pull off a similar trick with the body class, but I haven't been successful in adapting it:

    function wpprogrammer_post_name_in_body_class( $classes ){
 if( is_singular() )
 {
  global $post;
  array_push( $classes, "{$post->post_type}-{$post->post_name}" );
 }
 return $classes;
}

add_filter( 'body_class', 'wpprogrammer_post_name_in_body_class' );

A bit more crudely, I thought about using the_terms function to create my own classes for the custom posts, something like this:

<div class="<?php the_terms( $post->ID, 'taxonomy', '', ' ', '' ); ?>"></div>

But then I'd have to filter out the HTML that the_termthe_term generates.

Am I missing anything obvious here, is there a simpler way to solve this issue?

[1]: http://www.wprecipes.com/wordpress-hack-automatically-add-post-name-to-the-body-class);

I was surprised to learn that custom taxonomies aren't added as body or post classes like categories and tags are.

I'm sure this will be added in a future version of WordPress, but in the meantime I need to add a custom taxonomy to the post class so that I can style post in a certain category in that taxonomy differently.

It'd be most elegant to filter the post class and add the taxonomies to it. [I found a snippet][1] to pull off a similar trick with the body class, but I haven't been successful in adapting it:

    function wpprogrammer_post_name_in_body_class( $classes ){
 if( is_singular() )
 {
  global $post;
  array_push( $classes, "{$post->post_type}-{$post->post_name}" );
 }
 return $classes;
}

add_filter( 'body_class', 'wpprogrammer_post_name_in_body_class' );

A bit more crudely, I thought about using the_terms function to create my own classes for the custom posts, something like this:

<div class="<?php the_terms( $post->ID, 'taxonomy', '', ' ', '' ); ?>"></div>

But then I'd have to filter out the HTML that the_term generates.

Am I missing anything obvious here, is there a simpler way to solve this issue?

[1]: http://www.wprecipes.com/wordpress-hack-automatically-add-post-name-to-the-body-class);

I was surprised to learn that custom taxonomies aren't added as body or post classes like categories and tags are.

I'm sure this will be added in a future version of WordPress, but in the meantime I need to add a custom taxonomy to the post class so that I can style post in a certain category in that taxonomy differently.

It'd be most elegant to filter the post class and add the taxonomies to it. [I found a snippet][1] to pull off a similar trick with the body class, but I haven't been successful in adapting it:

function wpprogrammer_post_name_in_body_class( $classes ){
 if( is_singular() )
 {
  global $post;
  array_push( $classes, "{$post->post_type}-{$post->post_name}" );
 }
 return $classes;
}

add_filter( 'body_class', 'wpprogrammer_post_name_in_body_class' );

A bit more crudely, I thought about using the_terms function to create my own classes for the custom posts, something like this:

<div class="<?php the_terms( $post->ID, 'taxonomy', '', ' ', '' ); ?>"></div>

But then I'd have to filter out the HTML that the_term generates.

Am I missing anything obvious here, is there a simpler way to solve this issue?

[1]: http://www.wprecipes.com/wordpress-hack-automatically-add-post-name-to-the-body-class);

Source Link
JLeuze
  • 208
  • 1
  • 4
  • 9

Add post classes for custom taxonomies to custom post type?

I was surprised to learn that custom taxonomies aren't added as body or post classes like categories and tags are.

I'm sure this will be added in a future version of WordPress, but in the meantime I need to add a custom taxonomy to the post class so that I can style post in a certain category in that taxonomy differently.

It'd be most elegant to filter the post class and add the taxonomies to it. [I found a snippet][1] to pull off a similar trick with the body class, but I haven't been successful in adapting it:

    function wpprogrammer_post_name_in_body_class( $classes ){
 if( is_singular() )
 {
  global $post;
  array_push( $classes, "{$post->post_type}-{$post->post_name}" );
 }
 return $classes;
}

add_filter( 'body_class', 'wpprogrammer_post_name_in_body_class' );

A bit more crudely, I thought about using the_terms function to create my own classes for the custom posts, something like this:

<div class="<?php the_terms( $post->ID, 'taxonomy', '', ' ', '' ); ?>"></div>

But then I'd have to filter out the HTML that the_term generates.

Am I missing anything obvious here, is there a simpler way to solve this issue?

[1]: http://www.wprecipes.com/wordpress-hack-automatically-add-post-name-to-the-body-class);