- This topic has 13 replies, 6 voices, and was last updated 1 year, 9 months ago by
David.
-
AuthorPosts
-
April 14, 2017 at 5:53 am #305855
Chad Biggs
How is the best way to add a body class for a specific page?
GeneratePress 1.3.46GP Premium 1.2.96April 14, 2017 at 6:50 am #305869Jamal
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.
Agressively support the kind of work you want to see. Buy it. Talk about it. Review it.
April 14, 2017 at 11:26 am #305991Tom
Lead DeveloperLead DeveloperYep, that’s perfect π
For a specific page, you would replace
is_shop()
withis_page( 'page-slug' )
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/
Ongoing Development: https://generatepress.com/ongoing-developmentApril 15, 2017 at 4:02 am #306190Chad Biggs
Thank you Jamal and Tom.
April 15, 2017 at 9:11 am #306303Tom
Lead DeveloperLead DeveloperGlad we could help π
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/
Ongoing Development: https://generatepress.com/ongoing-developmentJanuary 22, 2019 at 9:33 am #789177Jonathan
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 #789287Leo
StaffCustomer SupportAny reason you don’t want to use the unique
.page-id-xxx
class that comes with each page?It should work exactly the same.
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/January 23, 2019 at 2:52 am #789766Jonathan
Semantic reasons. The class .page-id-xxx is meaningless while I .about-us is clear.
January 23, 2019 at 3:03 am #789773Jonathan
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 #790157Leo
StaffCustomer SupportAwesome π
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/April 26, 2019 at 2:27 am #880099Jonathan
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 #880312David
StaffCustomer SupportHi 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.Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/May 3, 2019 at 5:46 am #887759Jonathan
Thanks. That worked well.
May 3, 2019 at 6:01 am #887775David
StaffCustomer SupportYou’re welcome
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/ -
AuthorPosts
- You must be logged in to reply to this topic.