Within my header.php file, I need to set the body ID dynamically depending on the currently used template for whatever page someone happens to be viewing.
If someone is viewing a page that uses template A the body tag should look like this:
<body id="page1">
If someone is viewing a page that uses template B the body tag should look like this:
<body id="page2">
What would be the downside to me setting the following variable within my themes functions.php
$GLOBALS['my_current_body_id'];
Then at the beginning of each template I set the following:
$GLOBALS['my_current_body_id'] = '1';
or
$GLOBALS['my_current_body_id'] = '2';
and so on depending on how I need to style and structure the particular page.
Then in my header.php file I do the following:
<body id="page<?php echo $GLOBALS['my_current_body_id']?>">
I am wondering if there is a problem with using this type of approach that I might be overlooking.
For example, if two people hit the site at the exact same time, could there be a mix up or is the $GLOBALS[‘my_current_body_id’] specific to the user viewing the page?
Thanks.
How about this solution:
PHP:
Header.php:
The code I wrote feels a bit more simple, however…
If user A and user B are viewing the same page, they will see the same template.
But if user A is viewing the page with ID 4 and user B is viewing the page with ID 5, then they will see different templates.
Are you trying to make a plugin for A/B testing?
Here’s a couple methods, depending on what’s needed:
1) To grab the ‘post_name’ of the direct parent to the Page:
And to display the parent Page’s name:
2) To collect it for the top parent, that is the one at the top of a Page parent<>child heirarchy, we can modify the code from jpepper’s link:
Then use the same method as above to display the name.
For better way you can simply use it a function in functions.php and use it in header.php file.
Source : https://wordpress.org/support/topic/dynamic-id-for-ltbodygt-and-subpages