Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to insert the slugs of custom taxonomies as classes in the opening body tag on certain WordPress pages. What I have so far is causing errors.

I found a bit of help with the examples here and here, and it works, except that it gives an error on any page that doesn't have a term from the "section" taxonomy.

The error is:

Warning: join() [function.join]: Invalid arguments passed in /home/*****/public_html/example.com/wp-includes/post-template.php on line 387

and the code is:

// add classes to body based on custom taxonomy ('sections')
// examples: section-about-us, section-start, section-nyc
function section_id_class($classes) {
    global $post;
    $section_terms = get_the_terms($post->ID, 'section');
    if ($section_terms && !is_wp_error($section_terms)) {
        $section_name = array();
        foreach ($section_terms as $term) {
            $classes[] = 'section-' . $term->slug;
        }
    return $classes;
    }
}
add_filter('body_class', 'section_id_class');
share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

What is the job of $section_name? Also you must get an return; you if statement kill the default return. It is important, if your if statement fails.

maybe this works, but not tested, write from scratch.

add_filter( 'body_class', 'section_id_class' );
// add classes to body based on custom taxonomy ('sections')
// examples: section-about-us, section-start, section-nyc
function section_id_class( $classes ) {
    global $post;

    $section_terms = get_the_terms( $post->ID, 'section' );
    if ( $section_terms && ! is_wp_error( $section_terms ) ) {
        foreach ($section_terms as $term) {
            $classes[] = 'section-' . $term->slug;
        }
    }

    return $classes;
}
share|improve this answer
    
So you just moved the add_filter() to the top? That did work. Thanks for the tip. –  Josh2012 Aug 1 '12 at 9:46
    
Yes, is the same for functionality, but not for codex of WP. Process: got to hook body_class and include function section_id_class --> use the functions. The idea for this spelling style. –  bueltge Aug 1 '12 at 10:22
add comment

It's normal you'll get errors for pages that are not association with the section. Make sure you also return a '$classes' if your 'if-statement' fails.

In other words, make sure you add an else here.

// add classes to body based on custom taxonomy ('sections')
// examples: section-about-us, section-start, section-nyc
function section_id_class($classes) {
global $post;
$section_terms = get_the_terms($post->ID, 'section');
if ($section_terms && !is_wp_error($section_terms)) {
    $section_name = array();
    foreach ($section_terms as $term) {
        $classes[] = 'section-' . $term->slug;
    }
else 
{  $classes = 'default-class'; }
return $classes;
}
}
add_filter('body_class', 'section_id_class');

something like this

share|improve this answer
2  
on this example yous kill the default classes from WP and also the classes from other plugins or theme. –  bueltge Jul 31 '12 at 14:19
    
I agree, but that was why you got the error. You should make sure the replacement is only called for terms with section then. instead of on all. and drop the else then. and add filter when needed –  woony Aug 1 '12 at 9:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.