Custom Body Classes

This topic contains 1 reply, has 2 voices, and was last updated by  Paolo 8 years, 6 months ago.

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket
  • Author
    Posts
  • #364776

    C C
    Free User
    Post count: 3

    How would I go about adding a custom body class based on the Geodirectory Places Category?

    The in_category function does not appear to detect the categories?

    Any help would much appreciated.

    I’ve been attempting to use the code from the following:

    https://wpgeodirectory.com/add-custom-body-classes-to-wordpress/

    function my_custom_body_class_in_category($classes) {
    // add ‘my-class’ to the my-category archive and single posts of the category.
    if ( in_category( ‘my-category’ ) )
    $classes[] = ‘my-class’;
    // return the modified $classes array
    return $classes;
    }

    // add my custom class via body_class filter
    add_filter(‘body_class’,’my_custom_body_class_in_category’);

    #364914

    Paolo
    Site Admin
    Post count: 31211

    That example is for a WP blog archive, not for GeoDirectory CPTs.

    For custom taxonomies you will use has_terms:

    Try this for a gd_place CPT category:

    
    
    
    function my_custom_body_class_in_category($classes) {
        // add 'my-class' to the my-category archive and single posts of the category. 
        if ( has_term('my-category', 'gd_placecategory') )
            $classes[] = 'my-class';
        // return the modified $classes array
        return $classes;
    }
     
    // add my custom class via body_class filter
    add_filter('body_class','my_custom_body_class_in_category');

    Thanks

Viewing 2 posts - 1 through 2 (of 2 total)

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket
20% Discount Offer
Hurry! Get your 20% discount before it expires. Get 20% Discount