ThemeShaper Forums » Development Talk

[closed]

Body and Post Classes for Custom Taxonomies

(4 posts)
  1. umberto
    Member

    I'm trying to get post and body classes working for custom taxonomies using the latest thematic (rev 776) and WP 3.2.1.

    From what I have read in the changelog, 'Post and Body classes for custom taxonomies' were added back in 0.9.7.4 and there is a bunch of code in dynamic-classes.php that looks the part:

    in function thematic_body_class around line 100 :

    // Adds taxonomy classes for each term on single posts
    			$single_post_type = get_post_type_object(get_post_type($post->ID));
    
    			// Check for post types without taxonomy inclusion
    			if ( isset($single_post_type->taxonomy) ) {
    			    if ( $tax = get_the_terms($post->ID, get_post_taxonomies() )) {
    			    	foreach ( $tax as $term )   {
    			    		// Remove tags and categories from results
    			    		if  ( $term->taxonomy != 'post_tag' )	{
    			    			if  ( $term->taxonomy != 'category' )   {
    			    				$c[] = 's-tax-' . $term->taxonomy;
    			    				$c[] = 's-' . $term->taxonomy . '-' . $term->slug;
    			    			}
    			    		}
    			    	}

    around line 180,

    // Taxonomy name classes for BODY on tag archives
    
    		elseif ( is_tax() && apply_filters('thematic_show_bc_taxonomyarchives', TRUE)) {
    			$c[] = 'taxonomy';
    			$c[] = 'tax-' . $taxonomy;
    			$c[] = $taxonomy . '-' . strtolower(thematic_get_term_name());
    		}

    and in function thematic_post_class around line 430,

    if (function_exists('get_post_type_object')) {
    			// Taxonomies and terms for the post queried
    			$single_post_type = get_post_type_object(get_post_type($post->ID));
    			// Check for post types without taxonomy inclusion
    			if ( isset($single_post_type->taxonomy) ) {
    				foreach ( (array) get_the_terms( $post->ID, get_post_taxonomies() )  as $term  )   {
    					// Remove tags and categories from results
    					if  ( $term->taxonomy != 'category' )	{
    						if  ( $term->taxonomy != 'post_tag' )   {
    							$c[] = 'p-tax-' . $term->taxonomy;
    							$c[] = 'p-' . $term->taxonomy . '-' . $term->slug;
    						}
    					}

    However, I've test this on a stripped back install, on several earlier thematic versions and am only seeing classes for custom taxonomy archive pages.

    I may be mistaken but in light of the above was expecting taxonomy classes in much the same manner as category and tag classes.

    So I'd love some feedback:

    1. Is this is a known bug (which would seem odd given it was introduced a while ago)
    2. Is this is known to work as expected and the problem is somewhere on my end - highly likely I'm missing the obvious :)
    3. Although I don't think it's made it to WP core yet, this seems like a handy/essential bit of functionality. There are some solutions out there e.g. this from http://creersitepro.com/2011/post_type_css_taxonomy/

    function custom_fix_post_type_classes($classes, $class, $ID) {
    	$post = get_post($ID);
    	$taxonomies = ($post->post_type != 'post' && $post->post_type != 'page' && $post->post_type != 'attachement') ? get_object_taxonomies($post->post_type) :  false ;
    	if(is_array($taxonomies)) {
    		foreach($taxonomies as $taxonomy) {
    			$terms = get_the_terms( $post->ID, $taxonomy );
    			if(is_array($terms)) {
    				foreach($terms as $term) {
    					$classes[] .= ' '.$taxonomy .'-'. $term->slug ;
    				}
    			}
    		}
    	}
    	return $classes;
    }
    add_filter('post_class','custom_fix_post_type_classes',10,3);

    which I haven't quite managed to adapt to thematic yet. I will press with this (using my beginner/improving php skills) as I need to find a solution. Has anyone encountered and/or resolved this same issue?

    Appreciate any pointers.

    Posted 11 years ago #
  2. umberto
    Member

    Removing if ( isset($single_post_type->taxonomy) ) from the body class and the post class functions in dynamic-classes.php (line 432 & 106 in rev776) resolves this for me at this point.

    A temporary fix until I can work out what's going on. Could be my custom taxonomies are not set up properly?

    Any insights welcome

    Posted 11 years ago #
  3. middlesister
    Member

    Congratulations, you have found a bug!

    I found this weird and dug around a bit. Doing a print_r on the $single_post_type->taxonomy gives the error
    PHP Notice: Undefined property: stdClass::$taxonomy
    So of course the rest of the classes never gets added, since the taxonomy property is not set. Turns out it needs to be 'taxonomies', not 'taxonomy'.

    Don't remove those lines, change them to if ( isset($single_post_type->taxonomies) ) and it will work. I will file a bug for this.

    Posted 11 years ago #
  4. umberto
    Member

    Thanks middlesister
    Nice to have that sorted :)

    Posted 11 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.