alexanderdejong.com

Wordpress Developer

  • Home
  • Blog
  • Projects
  • Contact
You are here: Home / Wordpress

Add Custom Taxonomies to the Body Class

Posted on May 9, 2015 4 Comments

As of WordPress 4.2-alpha-31271 (23 January 2015), CSS classes for custom taxonomy terms are now automatically added by WordPress core when using post_class(). However, they are not automatically attached to the body classes.

You can use the following utility function to add your custom taxonomies to the body class for use as CSS hooks in themes:

/**
* Add Custom Taxonomy Terms To The Body Class
*/

add_filter( 'body_class', 'atp_custom_taxonomy_body_class', 10, 3 );

if ( ! function_exists('atp_custom_taxonomy_body_class') ) {
    function atp_custom_taxonomy_body_class($classes, $class, $ID) {

        $taxonomies_args = array(
            'public' => true,
            '_builtin' => false,
        );

        $taxonomies = get_taxonomies( $taxonomies_args, 'names', 'and' );

        $terms = get_the_terms( (int) $ID, (array) $taxonomies );

        if ( ! empty( $terms ) ) {
            foreach ( (array) $terms as $order => $term ) {
                if ( ! in_array( $term->slug, $classes ) ) {
                    $classes[] = $term->slug;
                }
            }
        }

        $classes[] = 'clearfix';

        return $classes;
    }
}

Put the above code in your theme’s functions.php file. Then, whenever the body_class() is used in a template:

<body <?php body_class();?> >

It will output any public custom taxonomy term(s) attached to the post, in addition to all of the default body classes.

Filed Under: Wordpress, WP Tutorials Tagged With: body_class, CSS, custom taxonomies body class, get_taxonomies

Recent Posts

  • Migrating a WordPress Website Manually with Phpmyadmin, Search and Replace, DigitalOcean and Runcloud
  • How to add Total Price Calculation to your WooCommerce Product Page with AJAX
  • How to Install Roots Bedrock on a Digital Ocean Droplet running on Serverpilot via SSH
  • Linux SSH Useful Server Commands to Remember: Grep, Permissions and more.
  • How to Install Roots Sage 8.5.1 on a Digital Ocean Droplet running on Serverpilot via SSH

Find more content

Affiliate links
  • cheap shared hosting
  • low priced virtual servers only 5$
Affiliates
  • If someone buys a product then I get a commission.
  • You pay the same price using my affiliate links.
  • I can spend more time making newsletters and tutorials.
  • Thanks for visiting!

Connect with me

Recent Comments

  • Pedro Gómez on Migrating a WordPress Website Manually with Phpmyadmin, Search and Replace, DigitalOcean and Runcloud
  • Tim Delmeire on How to add Total Price Calculation to your WooCommerce Product Page with AJAX
  • Jaehyuk J Yang on How to add Total Price Calculation to your WooCommerce Product Page with AJAX
  • Alexander de Jong on A guide on customising the membership experience with Theme my Login (TML)
  • Judit Sarkany on A guide on customising the membership experience with Theme my Login (TML)

Recent Posts

  • Migrating a WordPress Website Manually with Phpmyadmin, Search and Replace, DigitalOcean and Runcloud
  • How to add Total Price Calculation to your WooCommerce Product Page with AJAX
  • How to Install Roots Bedrock on a Digital Ocean Droplet running on Serverpilot via SSH
  • Linux SSH Useful Server Commands to Remember: Grep, Permissions and more.
  • How to Install Roots Sage 8.5.1 on a Digital Ocean Droplet running on Serverpilot via SSH

Copyright © 2014 | Alexander de Jong | About | Contact | Privacy | Sitemap