• I’m trying to figure out how to add add terms for pages to my body class automatically. I’m very close with some code I’ve been piecing together. Right now this works, but only returns one term. How can I get it to return an array of terms each as a class that get’s added to the body tag? Here’s what I’ve got so far. fyi “topbar” is my taxonomy name.

    <?php $class=”;
    if(is_page()) {
    $terms = get_terms(“topbar”);
    $class .= $terms[0]->slug;
    }?>
    <body id=”top” <?php if (function_exists(‘body_class’)) body_class($class ); ?>>

Viewing 3 replies - 1 through 3 (of 3 total)
  • try:

    <?php $class=array();
    if(is_page()) {
    $terms = get_terms("topbar");
    if( $terms ) foreach( $terms as $term ) { $class[] = $term->slug; }
    }?>
    <body id="top" <?php body_class($class ); ?>>

    (untested)
    (using wp3.2, there is no need to check if the body_class function exists)

    Thread Starter drhodes

    (@dthornborrow)

    That did it. Thanks alchymyth!

    Thread Starter drhodes

    (@dthornborrow)

    Dang I was wrong. That’s not quite it yet. What you suggested adds all the terms to the body tag. I would like it to only return the terms that are checked. For example there is a list of terms available , term1, term2, term3. If term2 is checked then add it to the body class, but don’t add 1 and 3.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘adding multiple terms to the body class’ is closed to new replies.