Skip to content

Instantly share code, notes, and snippets.

@cdils
Last active December 24, 2015 21:59
Show Gist options
  • Save cdils/6869728 to your computer and use it in GitHub Desktop.
Save cdils/6869728 to your computer and use it in GitHub Desktop.
Use within the loop (on a single post) to grab all taxonomy terms for post. Take each term and add it as a custom body class.
//* Add taxonomy term as body class to the header
add_filter( 'body_class', 'cd_body_class' );
function cd_body_class( $classes ) {
// Create array of terms within taxonomy named "group"
$terms = get_the_terms( $post->ID , 'group' );
// If there are no terms, bail
if ( $terms == null )
$return;
// Loop through terms array and assign as body class
foreach( $terms as $term ) {
$classes[] = $term->slug;
}
return $classes;
}
@curtismchale
Copy link

Why would you put this code inside the loop and not use it in the theme functions.php file? I suppose that if you put it in the loop it means you don't need to have a conditional of is_single() to make sure you're on a single post but that's about the only reason that I can think of.

@cdils
Copy link
Author

cdils commented Oct 7, 2013

Curtis - good point. In this case, I was using a single post template anyhow, so I added it directly. The code could be less-specific (i.e. don't name the taxonomy and dynamically grab it) for use outside the loop, yes?

@bradyvercher
Copy link

A couple of things I'd point out. The $post variable is never declared as a global here. It works because calling get_post() without an ID automatically fetches the current global post object. Instead, you can use get_the_ID() or you can use get_post()->ID anywhere after a post has been set up and is all get_the_ID() really does anyways.

get_the_terms() won't ever return null. It should return an array, false, or a WP_Error object. In almost all cases, if you want to check to see if something is empty, use the empty() function instead. Here, though, you'd do something like:

if ( ! $terms || is_wp_error( $terms ) ) ...

Also, that return statement isn't quite right ;)

Nothing wrong with a foreach loop, but you could also use wp_list_pluck( $terms, 'slug' ) and merge the result with the original classes.

With that in mind, you could do something like this (untested!):

<?php
// Add taxonomy term as body class to the header.
function cd_body_class( $classes ) {
    // Get an array of terms within the "group" taxonomy.
    $terms = get_the_terms( get_the_ID() , 'group' );

    // Bail if there are no terms.
    if ( ! $terms || is_wp_error( $terms ) ) {
        return $classes;
    }

    return array_merge( $classes, wp_list_pluck( $terms, 'slug' ) );
}
add_filter( 'body_class', 'cd_body_class' );

@cdils
Copy link
Author

cdils commented Oct 7, 2013

Brady,
Thanks so much for the break down and explanation.

Question: Why does ! $terms accomplish the same thing as an empty() check in your example? Your code worked perfectly (and introduced me to two new WP functions).

As for that original return, well, I just like extra dollars. ;)

Thank you!
Carrie

@bradyvercher
Copy link

No problem.

! $terms isn't the same as an empty() check (unless the value really is null), but if you look at the possible return values for get_the_terms(), they are array|bool|WP_Error. Looking at the function, it does its own check for an empty value and returns false, so you'll never receive an empty value. The ! $terms is just verifying that $terms isn't false.

Typically API functions should try to standardize their output and may do some error checking (and caching in this case) of their own to help keep your code a bit cleaner without having to check for more than a couple of response formats.

If figured as much regarding that return, who doesn't like extra dollars? Rather than just the return in a filter like this, you'd probably want to return the original $classes parameter so the existing body classes don't get blanked out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment