I am trying to create a body class name based on a specific page in php. What I need to do is first define a variable and then check if the variable exists, and then if it exists display that one, otherwise if it is something else, then do something else, or if nothing then do not show any.
<body<?php if (defined('PAGE_KEY') && var == "homepage") echo " class=\"homepage\"";
elseif (defined('PAGE_KEY') && var == "page3") echo " class=\"page3\"";
elseif (defined('PAGE_KEY') && var == "page12") echo " class=\"page12\""; ?>>
This code will be located in my
My thought was to first check if variable was defined and if so then check what the variable was defined as and then based on that variable it will display a respective class.
The goal is for example on the page12 for the body tag to look like this:
But for example for the body tag on page55 (which I don't want to show a class for) to look like this:
In doing so I am able to now define css specifically for a page within the header where the body tag happens to be located.
Problem is first I don't know how to define the variable in the page, and second I don't know exactly how to write the php code above properly.
Attempt, for example on page12 I would have this code:
<?php PAGE_KEY = "page12" ?>
This code would be for example located in
Also keep in mind, that the variable will come AFTER the body tag.
I also thought of trying to see what the page URL is but I think that's just making things too complicated.
Based on @Jordi's suggestion, how about this:
<body class="<?php echo PAGE_KEY ?>">
And then on
<?php PAGE_KEY = "page12" ?>
and for example on
<?php PAGE_KEY = "page5" ?>
so that on those respective pages the body tag shows this:
the body tag will show this:
Is this right?
@Jose made this suggestion, is this correct?
for example, on
, to define the variable as 'page12', this:
<?php define("PAGE_KEY", "page12"); ?>
Is that what you were suggesting to do?
Ok, so it seems the problem I'm having is that since the code to define the page comes after
tag, which the is located inside head.php
and for example
code comes after
head.php` code, then it's not working because of that.
I looked in the code and it looks like for other things, the original page programmer did something different to circumvent this issue. I could be wrong, but it looks like first they stored the variable in the mysql database, for the page; and then in head.php checked to see if that was defined and if so then run code.
Here for example is a line of their code they used for other purposes, that is found on the FAQ page:
define("PAGE_NAME", t("faq_page_name", "FAQ"));
So, what I need to do is replicate it.
It is also worth noting that because of this, each page does have a unique identifier. For example, the FAQ page has the identifier "FAQ". But I would not know how to fetch that.
<body<?php echo " class=\"" . constant( "PAGE_NAME" ) . "\""; ?>>
This would make the class name the identifier; f course I need something unie so maybe I could append it, like this?
<body<?php echo " class=\"" . constant( "PAGE_NAME" ) . "\"-zz"; ?>>
So for like faq page which has the variable "FAQ" this would result in the class like this:
The only problem is that I am not sure if my code above is forrect for fetching the variable name.
I could work with this if I can get it to work. I would just only need to edit the stylesheet, i.e. instead of
I would change it to