Forums

adding multiple terms to the body class (4 posts)

  1. drhodes
    Member
    Posted 7 months ago #

    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 ); ?>>

  2. alchymyth
    The Sweeper
    Posted 7 months ago #

    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)

  3. drhodes
    Member
    Posted 7 months ago #

    That did it. Thanks alchymyth!

  4. drhodes
    Member
    Posted 7 months ago #

    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.

Reply

You must log in to post.

About this Topic