- This topic has 21 replies, 5 voices, and was last updated 3 years, 2 months ago by
David.
-
AuthorPosts
-
December 19, 2022 at 6:04 pm #2467649
Charles
In a different child theme, I could add body attributes from the top of a custom page template (custom-template.php for example). After moving it over to generate press, I can’t find a way to hook in from the top of a custom template to add this. For example, I am trying to add <body id=”XXX” data-spy=”scroll” data-target=”.scrollspy”>.
December 19, 2022 at 6:33 pm #2467661Fernando Customer Support
Hi Charles,
To do this, you’ll need to use the GP Child theme: https://docs.generatepress.com/article/using-child-theme/
Then, add make a copy of the
header.phpfile in your child theme: https://github.com/tomusborne/generatepress/blob/master/header.phpThen, edit the <body> tag and add your attributes.
December 19, 2022 at 6:47 pm #2467667Charles
Yes I am using a child theme.
.... </head> <body <?php body_class(); ?> <?php generate_do_microdata( 'body' ); ?>> <?php /** * wp_body_open hook. .....But I do not want to attribute on every <body> element. I would just like to hook from my page template into the body element. That way, every page that uses that custom page template will have the correct attributes. The previous framework parent theme I was using just let you write it at the top and would hook into it. Wondering if there is a way I can do that with GeneratePress.
December 19, 2022 at 7:21 pm #2467684Fernando Customer Support
You can change the body tag to this:
<body <?php body_class(); ?> <?php generate_do_microdata( 'body' ); echo apply_filters('body_custom_attributes', ''); ?>>With this, you can use a filter to add attributes. For instance, to add the attributes to a specific page, you can add this:
function wp_body_custom_attributes( $atts ) { if( is_page('147') && ! is_admin() ) { return 'ID="123" data-spy="scroll" data-target=".scrollspy"'; } return $atts; } add_filter( 'body_custom_attributes','wp_body_custom_attributes', 999 );Replace
147with your page ID.Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets
To get the Page/Post ID, edit the page. In the URL link, the ID should be there.
December 19, 2022 at 7:29 pm #2467692Charles
That is just what I am looking for. Thank you Fernand for the help.
December 19, 2022 at 7:33 pm #2467695Fernando Customer Support
You’re welcome, Charles!
January 24, 2023 at 6:49 am #2507123Vincenzo
I Fernando. I tried your code with the filter to insert an attribute inside body tag but it seems doesn’t work at all. Among other things i did a search on body_custom_attributes filter but i didn’t find any documentaion either as a WordPress filter or as a Generatepress filter. If it worked it would be a very useful tool.
January 24, 2023 at 7:33 am #2507174David
StaffCustomer SupportHi there,
you would need to follow the entire topic, as it needs a child theme to edit the themes header.php and add the filter in there. Otherwise it doesn’t exist.
Alternatively you can hijack the
generate_{$context}_microdatafilter:1. First add this snippet
add_filter('generate_body_microdata', function($data){ $att = ''; $custom_data = apply_filters( 'db_custom_body_attribute', $att ); $data .= ' ' . $custom_data; return $data; });This creates a new
db_custom_body_attributefilter hook immediately after the microdata in thebody2. Which you can then use like this:
add_filter('db_custom_body_attribute', function($att){ $att = 'data-attr="data rocks"'; return $att; });January 24, 2023 at 7:46 am #2507194Vincenzo
Ok. Thank you. Sorry for the distraction.
January 24, 2023 at 8:09 am #2507394David
StaffCustomer SupportNo apologies necessary – try my method – let me know if theres something specific you require.
January 25, 2023 at 3:01 am #2508313Vincenzo
I David. I’ve tried your method and it works fine.
I’ve used it to insert the ‘data-scroll-container’ attribute in the body tag to implement Locomotive scroll library on a site developped with Generatepress e Generateblock.
But, beyond my distraction, i was wondering if this line in the previous code is correct:return 'ID="123" data-spy="scroll" data-target=".scrollspy"';January 25, 2023 at 3:52 am #2508373David
StaffCustomer SupportCorrect for what ? 🙂
Sorry i am not familiar with what attributes you need to add.January 25, 2023 at 4:02 am #2508384Vincenzo
it shouldn’t be?:
$atts = ‘ID=”123″ data-spy=”scroll” data-target=”.scrollspy”‘;January 25, 2023 at 4:12 am #2508394David
StaffCustomer SupportIn my second code:
add_filter('db_custom_body_attribute', function($att){ $att = 'data-attr="data rocks"'; return $att; });You can change that to:
add_filter('db_custom_body_attribute', function($att){ $att = 'ID="123" data-spy="scroll" data-target=".scrollspy"'; return $att; });January 25, 2023 at 4:27 am #2508411Vincenzo
Sorry David, maybe I didn’t explain myself well, I was referring to the code of Fernando.
-
AuthorPosts
- You must be logged in to reply to this topic.