- This topic has 6 replies, 3 voices, and was last updated 6 months, 3 weeks ago by
Fernando.
-
AuthorPosts
-
August 28, 2022 at 7:20 am #2326162
Angie
Hello!
I want to give my client an easy possibility to change the background image of the body of each page. My approach is to add a class to the body tag. I found this support topic and wanted to adapt it for my purpose, maybe you could help?
https://generatepress.com/forums/topic/body-class-for-categories-and-acf-field/So I added a costum field to the pages with a dropdown menu, where the client can choose a class-name, that should be added to the body tag. With costum CSS I set the corresponding background image.
The field name is: bild-header
CSS:
body.eichhorn { background-image:url("http://b3q5oy.myraidbox.de/wp-content/uploads/2022/07/images_HOME-squirrel_screen600.jpg"); background-position: right top; background-repeat: no-repeat; background-size: 40vw; }
Now I took the code from the support topic and added it to functions.php: But how can I adapt it, so it just adds the fields content as class to the body tag? I guess I don’t need the elseif that refers to categories?
add_filter('body_class','add_category_to_single'); function add_category_to_single($classes) { global $post; $value = get_field('bild-header'); // The ACF Field if ( is_page() ) { $classes[] = $value; // Supposed to take the ACF Field Value } elseif (is_singular() ) { foreach((get_the_category($post->ID)) as $category) { $classes[] = $category->category_nicename . $value; } } return $classes; }
Thank you very much!
Angie.September 2, 2022 at 4:34 am #2331354Angie
Hi again! Does someone have an idea for this? Thank you very much! Angie.
September 2, 2022 at 5:24 am #2331398David
StaffCustomer SupportHi there,
sincere apologies for missing your original topic, lets take a look.
Try this:
add_filter( 'body_class', 'add_acf_body_class' ); function add_acf_body_class( $classes ) { global $post; ) // Get the ACF Field $value = get_field( 'bild-header' ); // The ACF Field // Check if $value exists if ( $value ) { // load $classes with ACF value $classes[] = $value; } return $classes; }
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/September 5, 2022 at 1:41 pm #2334221Angie
Thank you! and please don’t apologise for your amazing support!!
Could it be, that there is a bracket missing?
Angie.September 5, 2022 at 7:06 pm #2334337Fernando Customer Support
Hi Angie,
Can you try this instead?:
add_filter( 'body_class', 'add_acf_body_class' ); function add_acf_body_class( $classes ) { global $post; // Get the ACF Field $value = get_field( 'bild-header' ); // The ACF Field // Check if $value exists if ( $value ) { // load $classes with ACF value $classes[] = $value; } return $classes; }
September 5, 2022 at 11:29 pm #2334442Angie
Yes! This made it work. Thank you so much again!!
Angie.September 5, 2022 at 11:38 pm #2334448Fernando Customer Support
You’re welcome Angie!
-
AuthorPosts
- You must be logged in to reply to this topic.