ThemeShaper Forums » Thematic

[closed]

Showing custom taxonomies for pages in the body tag

(11 posts)
  • Started 6 years ago by butters81
  • Latest reply from butters81
  • This topic is resolved
  1. butters81
    Member

    Hi guys,

    I feel this should be easy, but I'm tying myself up in knots trying to solve this! Ok, so plan is - on my homepage, I'm using Justin Tadlock's great Query Posts plugin. In the subsidiary asides, I'm getting the plugin to display a random *page* (not post).

    In order to style the pages differently (e.g. different colours for H1, H2 etc. depending on the section), I'm using a custom post type - I've basically enabled tags for pages. However, how do I get custom tags to show up in the body class for pages? This has worked fine in the past for posts, but I'm stumped for pages.

    I've got as far as (where "section" is my custom taxonomy - tags for pages):

    function extend_body_class($c) {
    $c[] = get_the_term_list($post->ID, 'section');
    return $c;
    }
    add_filter('thematic_body_class', 'extend_body_class');

    But it outputs "Array" into the body tag. Arggh. Any help gratefully appreciated!

    Posted 6 years ago #
  2. well get_the_term_list will return an array. you could try imploding the array

    function extend_body_class($c) {
    $terms = get_the_term_list($post->ID, 'section');
    if($c) $c[] = implode(" ", $terms);
    return $c;
    }
    add_filter('thematic_body_class', 'extend_body_class');

    totally untested

    Posted 6 years ago #
  3. butters81
    Member

    Kicks up a warning error ("invalid arguments passed in ... on line 241"). I've looked at the implode function and definitely looks like it's along the right lines though...

    Posted 6 years ago #
  4. probably need to check that $terms exists and is an array? or check that you are on the type of page you need/want to add body classes for. bottom line i think you need some kind of condition to keep this from firing on every page on your website. tax archives, date archives, etc won't have a get_the_term_list() so implode would fire an error. i think.

    Posted 6 years ago #
  5. butters81
    Member

    This kinda works, but is a bit cludgy. It checks for the presence of the term/tag "goodpractice" in the "section" taxonomy, and then sticks some text in the body class. It will be manageable as I've only got 5 tags I want to check for, but implode (above) escaped me...

    function extend_body_class($c) {
    if (has_term('goodpractice', 'section')) {
    $c[] = "section-goodpractice";
    return $c;
    }
    else return $c;
    }
    add_filter('thematic_body_class', 'extend_body_class');

    Posted 6 years ago #
  6. also note that if you disable thematic_body_class (ie don't enable it in your functions.php) then wordpress adds taxonomy/term info automatically to body_class(). all the thematic dynamic classes are being deprecated as of the version in the SVN, which is going to eventually be submitted to the WP repo

    Posted 6 years ago #
  7. butters81
    Member

    That would be a shame - Thematic adds a lot more body tags!

    Posted 6 years ago #
  8. not really. when the discussion came up on google code, i did a comparison and there were only a few classes (and ones that i have never used) that thematic adds that wordpress does not. if you're curious... check the body classes with

    define('THEMATIC_COMPATIBLE_BODY_CLASS', true);

    and then check again w/o it.

    wordpress is adding taxonomy and post format stuff that thematic is way behind on. it made more sense to let WP handle the job instead of constantly playing catch up. there will be the ability to add_theme_support for legacy classes, but i don't see the point.

    if you are reliant on some class that thematic adds but body_class() does not you can filter body_class in exactly the same way you are filtering thematic_body_class.

    also, i just realized that you can't implode the result of get_the_term_list() since it is returns a string....and a string with html links at that.

    Posted 6 years ago #
  9. define('THEMATIC_COMPATIBLE_BODY_CLASS', true);
    
    function extend_body_class($c) {
    	global $post;
    
    	$terms = get_the_terms($post->id, 'section');
    
    	if($terms) foreach ($terms as $term):
    		$c[] = $term->slug;
    	endforeach;
    
    	return $c;
    }
    add_filter('thematic_body_class', 'extend_body_class');

    this works for me (with a different tax that i actually have enabled of course)

    Posted 6 years ago #
  10. helga, any news from gene?

    Posted 6 years ago #
  11. butters81
    Member

    Thanks! That function's much more elegant (plus will work if I add any more tags in the future). Comparing the thematic_compatible_body_classes, this is the difference I get:

    false (i.e. WP default):
    page page-id-30 page-child parent-pageid-18 page-template page-template-template-page-fullwidth-php windows chrome ch19

    true (i.e. thematic):
    wordpress blogid-1 y2012 m04 d21 h10 singular slug-8-key-themes page pageid-30 page-author-movement page-child parent-pageid-18 page-has-excerpt page-comments-open page-pings-closed page-template page-template-template-page-fullwidth-php section-research windows chrome ch19

    I've used the singular; slug; page-author classes to target styles before - but I see what you mean, there are quite a few which I wouldn't use (but are often quite handy to see at a glance some of the details of the page).

    Posted 6 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.