Site logo

[Resolved] Show Custom Body class on Pages and Posts

Home Forums Support [Resolved] Show Custom Body class on Pages and Posts

Home Forums Support Show Custom Body class on Pages and Posts

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #2100304
    Giselle

    Hi,

    I’ve created a custom body class.

    I added a snippet to get it to show on Pages and this is working. However, I need the class on Posts on Pages. I tried Tom’s solution, but it isn’t working:
    https://generatepress.com/forums/topic/custom-css-body-class-field/

    Can I add code to the below to show both?

    THIS IS WORKING, BUT ONLY ON PAGES

    add_filter( ‘body_class’, ‘custom_body_class’ );
    /**
    * Add custom field body class(es) to the body classes.
    *
    * It accepts values from a per-page custom field, and only outputs when viewing a singular static Page.
    *
    * @param array $classes Existing body classes.
    * @return array Amended body classes.
    */
    function custom_body_class( array $classes ) {
    $new_class = is_page() ? get_post_meta( get_the_ID(), ‘body_class’, true ) : null;

    if ( $new_class ) {
    $classes[] = $new_class;
    }

    return $classes;
    }

    Thanks for your help!

    Giselle

    #2100326
    David
    Staff
    Customer Support

    Hi there,

    Tom’s method should still work.
    For reference this is the code:

    add_filter( 'body_class', 'tu_page_specific_body_class' );
    function tu_page_specific_body_class( $classes ) {
        if ( is_singular() && get_post_meta( get_the_ID(), 'page_class', true ) ) {
            $classes[] = get_post_meta( get_the_ID(), 'page_class', true );
        }
    
        return $classes;
    }

    In that code there are 2 x lines that reference the Custom Field Name: page_class
    You need to change them both to your specific custom field name

    #2100366
    Giselle

    Hi David,

    Thanks. Just tried it again and it isn’t working.

    There’s a body class set on narrow Pages at the moment called ‘text-column’. When I replace the snippet I had originally (the one just for Pages) with Tom’s code, the pages with that class go full-width and you can see the body class isn’t there.

    I’ve left it like that for now so you can see.

    Example page below.

    Thanks!
    Giselle

    #2100367
    Giselle

    The class is to make full-width pages narrow I mean.

    #2100372
    David
    Staff
    Customer Support

    OK heres your original code – i made a modification so its for any page or post:

    add_filter( 'body_class', 'custom_body_class' );
    /**
     * Add custom field body class(es) to the body classes.
     *
     * It accepts values from a per-page custom field.
     *
     * @param array $classes Existing body classes.
     * @return array Amended body classes.
     */
    function custom_body_class( array $classes ) {
    	$new_class = get_post_meta( get_the_ID(), 'body_class', true );
    
    	if ( $new_class ) {
    		$classes[] = $new_class;
    	}
    
    	return $classes;
    }
    #2100401
    Giselle

    Hi David,

    Sorry, I missed this:

    In that code there are 2 x lines that reference the Custom Field Name: page_class
    You need to change them both to your specific custom field name

    1. I tried this first. My class is called body_class. I changed it to page_class in case that was causing problems. Didn’t work.

    2. I then changed it back and tried your code and it’s working!

    Thanks so much! much appreciated.

    Giselle

    #2100405
    David
    Staff
    Customer Support

    Glad to be of help!

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.