I'm building a site with PHP and using an include for the footer with the following markup:
<footer id="main-footer>
<p>footer text</p>
</footer>
There are a few instances throughout the site for which I would like to modify the footer-- in a way that is specific to a given page-- with an additional class appended to the footer
element; for example, appending a class of "bio" to modify the appearance of the footer on the bio page.
Ideally, I'd like to configure the PHP such that a class is only added when necessary; thus, for the majority of pages throughout the site that do not require a custom footer, that no additional class would appear appended to the element.
How might I achieve this in PHP? Thanks for any feedback here.
It's a little broad because we don't know how you're including this, but the most straight-forward way in PHP is to check for the existance of a variable and including the class based on that. If the variable exists; include it, otherwise not.
Then any page that needs it can simply set the variable and it should work.
(If this isn't workable for you, please provide more information on how you include pages and such)
<footer <?php if( isset( $expandedFooter )) { echo 'class="' . $expandedFooter . '"'; } ?> id="main-footer">
Then, any page that includes this line will have the extra footer class:
$expandedFooter = "someClassName";
Any class that doesn't have it will not include the extra footer, but it also will not throw any warnings due to the missing variable because we're using isset rather then testing the variables actual value. (Which would generate a warning if the variable does not exist at all)