Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using php to show a different header logo image if body class is home?

I'm working on a website where the homepage has a dark background, yet all the other pages have a white background.

I am using pho to include a header file to show the logo, navbar, telephone details etc on every page.

As the home page has a dark background, the logo has white text, yet the logo use on the other pages has dark text.

I'm looking for a way of using php, so that I include a single header file on every page. If the homepage has a class of "home" the logo image with white text is shown and on all other pages the logo image with dark text is shown.

something along these lines:

if (body class="home") {

<img src="images/logo-with-white-text" />

else {

<img src="images/logo-with-dark-text" />

};

Is this possible?

Any help or advice would be greatly appreciated :)

like image 447
ade123 Avatar asked Dec 30 '25 16:12

ade123


2 Answers

I'm assuming your homepage currently looks something like this:

<html>
    <head>...</head>
    <body class="home">
        ...
        <?php include 'header.php'; ?>
        ...

You could make the class a variable, and reference this variable from the included header file:

<?php $class = 'home'; ?>
<body class="<?php echo $class; ?>">
...
<?php include 'header.php' ?>
...

In header.php:

<?php if (isset($class) && $class == 'home'): ?>
    <img src="images/logo-with-white-text" />
<?php else: ?>
    <img src="images/logo-with-dark-text" />
<?php endif; ?>
like image 69
nachito Avatar answered Jan 02 '26 07:01

nachito


You could check whether you are on the homepage (Depending on your exact implementation) with a snippet like this:

if (basename($_SERVER['SCRIPT_NAME']) == 'index.php') {
    // home page
}
else {
    // some other page
}

$_SERVER['SCRIPT_NAME'] contains the actually loaded file relative from the host until the query-string:

http://example.com/my/folder.php?a=b => /my/folder.php

For more information have a look at basename in the PHP manual.

like image 35
TimWolla Avatar answered Jan 02 '26 08:01

TimWolla