Home › Forums › Support › Body class This topic has 13 replies, 6 voices, and was last updated 4 years, 11 months ago by David. Viewing 14 posts - 1 through 14 (of 14 total) Author Posts April 14, 2017 at 5:53 am #305855 Chad Biggs How is the best way to add a body class for a specific page? April 14, 2017 at 6:50 am #305869 Jamal Hi Chad Looking at the documentation it looks like we can use the following add_filter( 'body_class','my_body_classes' ); function my_body_classes( $classes ) { if ( is_shop() ) { $classes[] = 'class-name'; $classes[] = 'class-name-two'; } return $classes; } https://code.tutsplus.com/tutorials/adding-to-the-body-class-in-wordpress–cms-21077 Hope that works for you. April 14, 2017 at 11:26 am #305991 TomLead Developer Lead Developer Yep, that’s perfect π For a specific page, you would replace is_shop() with is_page( 'page-slug' ) April 15, 2017 at 4:02 am #306190 Chad Biggs Thank you Jamal and Tom. April 15, 2017 at 9:11 am #306303 TomLead Developer Lead Developer Glad we could help π January 22, 2019 at 9:33 am #789177 Jonathan How would you add the page/post name (title) to the function so that you don’t have to hard code the class? i.e If I have an about page I would want the body to include .about as a class January 22, 2019 at 11:19 am #789287 LeoStaff Customer Support Any reason you don’t want to use the unique .page-id-xxx class that comes with each page? It should work exactly the same. January 23, 2019 at 2:52 am #789766 Jonathan Semantic reasons. The class .page-id-xxx is meaningless while I .about-us is clear. January 23, 2019 at 3:03 am #789773 Jonathan I used this piece of code in functions.php of my child theme to make the class: add_filter( 'body_class', 'sk_body_class_for_pages' ); function sk_body_class_for_pages( $classes ) { if ( is_singular( 'page' ) ) { global $post; $classes[] = 'page-' . $post->post_name; } return $classes; January 23, 2019 at 8:07 am #790157 LeoStaff Customer Support Awesome π April 26, 2019 at 2:27 am #880099 Jonathan Anyone know how to make a class based on the meta value? So if I create a custom field called ‘my-body-class’ and have a value of ‘whatever is put in here’ what do I need to show the value part? April 26, 2019 at 6:00 am #880312 DavidStaff Customer Support Hi there, try this: <?php function db_custom_field_body_class($classes) { if(get_post_meta(get_the_ID(), 'custom_body_class', true)) $classes[] = get_post_meta(get_the_ID(), 'custom_body_class', true); return $classes; } add_filter('body_class','db_custom_field_body_class'); ?> the custom field name is: custom_body_class or whatever you want to change it to. May 3, 2019 at 5:46 am #887759 Jonathan Thanks. That worked well. May 3, 2019 at 6:01 am #887775 DavidStaff Customer Support You’re welcome Author Posts Viewing 14 posts - 1 through 14 (of 14 total) You must be logged in to reply to this topic. Log In Username: Password: Keep me signed in Log In