0

I've been trying without luck to get a custom taxonomy slug to appear as a class in the body tag of WordPress. Perhaps it has to do with the fact that I'm using a custom taxonomy rather than the build in one.

I'm registering a custom PAGE taxonomy in functions.php as follows and it works fine.

add_action( 'init', 'create_page_taxonomies' );
function create_page_taxonomies() {
register_taxonomy('page_section', 'page', array(
    'hierarchical' => true,
    'labels' => array(
        'name' => _x( 'Page Section', 'taxonomy general name' ),
        'singular_name' => _x( 'page-section', 'taxonomy singular name' ),
        'search_items' =>  __( 'Search Sections' ),
        'all_items' => __( 'All Sections' ),
        'parent_item' => __( 'Parent Section' ),
        'parent_item_colon' => __( 'Parent Section:' ),
        'edit_item' => __( 'Edit Section' ),
        'update_item' => __( 'Update Section' ),
        'add_new_item' => __( 'Add New Section' ),
        'new_item_name' => __( 'New Section Name' ),
        'menu_name' => __( 'Sections' )
    ),
    'public' => true,
    'rewrite' => array(
        'slug' => 'page-section',
        'with_front' => false,
        'hierarchical' => true
    )
));
}

This creates a 'Sections' menu option under Pages. It works just like Post Categories. I then created a taxonomy Term labelled Trip Planner (slug: trip-planner) I then add the following to functions.php to try and get the taxonomy term slug name to appear as a class on the body tag.

add_filter( 'body_class', 'themeprefix_add_taxonomy_class' );
function themeprefix_add_taxonomy_class( $classes ){
if( is_singular() ) {
    global $post;
    $taxonomy_terms = get_the_terms($post->ID, 'page-section');
    if ( $taxonomy_terms ) {
        foreach ( $taxonomy_terms as $taxonomy_term ) {
        $classes[] = 'section-' . $taxonomy_term[0]->slug;
        }
    }
}
return $classes;
}

This second snippet is 'sort of' working, when added and I view page source, I can see 'section-' appear as a class on the body tag but the term slug value which is supposed to follow the dash is not present. My body tag now looks like this.

<body class="page-template page-template-elementor_header_footer page page-id-451 wp-custom-logo section- elementor-default elementor-template-full-width elementor-kit-6 elementor-page elementor-page-451">

Mostly Elementor page builder classes but the 'section-' is present. I edited the second function to reference the 'page-section' slug defined in the register_taxonomy function.

Additional info if helpful.

  • WordPress: 6.4.2
  • Page Builder: Elementor
  • Custom Page Taxonomy Slug: page-section
  • Taxonomy Term Name Example: Trip Planner
  • Taxonomy Term Slug Example: trip-planner
  • Expected Body Class to be Added: section-trip-planner

I must be missing something?!?

UPDATE:

The PHP error generated (see comment below) when I corrected the page-section to page_section let me to the solution. $taxonomy_term[0] is not an array it is an Std Object, therefor I just removed '[0]' and now everything works fine.

2
  • While looking over my code again, I may have found an issue. My custom Taxonomy is assigned the ID of 'page_section' and the slug is assigned the name of page-section. My code to add the body class is calling the taxonomy slug instead of it's ID. I modified my body class code to use page_section instead of page-section. That didn't work, instead of printing the body class the site just errors out with the message "There has been a critical error on this website.".
    – Overspeed
    Dec 19, 2023 at 23:02
  • When I change page-section to page_section and the site goes down with critical error, the following is shown in the PHP error log: [19-Dec-2023 23:20:23 UTC] PHP Fatal error: Uncaught Error: Cannot use object of type WP_Term as array in /home/j8k4tkm/public_html/wp-content/themes/hello-theme-child-master/functions.php:181
    – Overspeed
    Dec 19, 2023 at 23:22

2 Answers 2

0

To add the custom taxonomy slug to the body classes of your WordPress site, you can use the body_class filter along with your custom code. Below is your example of how you can achieve this:

function themeprefix_add_taxonomy_class($classes) {
    global $post;
    if (is_page()) {
        $taxonomy = 'page_section';
        $terms = get_the_terms($post->ID, $taxonomy);

        if ($terms && !is_wp_error($terms)) {
            foreach ($terms as $term) {
                $classes[] = 'section-' . $term->slug;
            }
        }
    }
    return $classes;
}
add_filter('body_class', 'themeprefix_add_taxonomy_class');
2
  • I replaced my code with yours, the term slug class was not added to the body tag. The 'section-' prefix was also not added. Looks like our code is nearly identical save the addition check you added for is_wp_error.
    – Overspeed
    Dec 19, 2023 at 18:27
  • To include a custom taxonomy slug in the body classes of a WordPress page (not a post), you can modify the code. I've made the necessary updates in my response. Dec 20, 2023 at 4:59
0

$taxonomy_terms is already an array, so you either do the foreach loop to add all the associated terms as seperate classes (in that case you don't need the first index, i.e 0) or add only the first term (in that case you don't need the loop).

To add all the terms..

add_filter( 'body_class', 'themeprefix_add_taxonomy_class' );
function themeprefix_add_taxonomy_class( $classes ){
if( is_singular() ) {
    global $post;
    $taxonomy_terms = get_the_terms($post->ID, 'page-section');
    if ( $taxonomy_terms && !empty($taxonomy_terms) ) {
        foreach ( $taxonomy_terms as $taxonomy_term ) {
        $classes[] = 'section-' . $taxonomy_term->slug;
        }
    }
}
return $classes;
}    

Or, just the first term..

add_filter( 'body_class', 'themeprefix_add_taxonomy_class' );
function themeprefix_add_taxonomy_class( $classes ){
if( is_singular() ) {
    global $post;
    $taxonomy_terms = get_the_terms($post->ID, 'page-section');
    if ( $taxonomy_terms && isset($taxonomy_term[0]) ) {
        $classes[] = 'section-' . $taxonomy_term[0]->slug;
    }
}
return $classes;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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