Question

I'm trying to change the header logo on my site, depending on the page the person is on. I don't know PHP, but I've found where the Logo is defined in header.php, and am trying to rewrite it to be dynamic. When I use my code, the site breaks, so obviously I'm doing something wrong.

The original code is:

<!-- Logo -->    
<?php
// Get the logo
if ($ti_option['site_logo'] != '') {
    $site_logo = $ti_option['site_logo'];
}
else {
    $site_logo = get_template_directory_uri() . '/images/logo.png';
}
?>

<a class="logo" href="<?php echo home_url('/'); ?>">
<img src="<?php echo $site_logo; ?>" alt="<?php bloginfo('name'); ?>
 - 
<?php bloginfo('description'); ?>" title="<?php bloginfo('name'); ?>
 - 
<?php bloginfo('description'); ?>" />
</a>
<!-- End Logo -->

What I'm trying to do is display a different logo based on which page the visitor is on. There are three:
- if the page is one of these: (1168, 1433, 1428), display /path/logo1.jpg
- if the page is one of these: (1369, 1361, 1365), display /path/logo2.jpg
- otherwise, just show /path/logo3.jpg

Here's what I've been able to manage so far:

    <!-- Logo -->
 <?php

// Get the logo

 < ? php
if ($ti_option['site_logo'] != '') {
    if (is_page(array(
        1168,
        1433,
        1428
    ))) :

    //                              '$site_logo' => 'FILELOCATION.jpg',

    $site_logo = $ti_option['site_logo'];));
elseif (is_page(array(
    1369,
    1361,
    1365
))):
    $site_logo = $ti_option['site_logo'];));
else:
    $site_logo = $ti_option['site_logo'];
endif;
?>

} else {
      $site_logo = get_template_directory_uri() . '/images/logo.png';
}
?>
<a class="logo" href="<?php echo home_url('/'); ?>">
<img src="<?php echo $site_logo; ?>" alt="<?php  bloginfo('name'); ?>
 - 
<?php bloginfo('description'); ?>" title="<?php  bloginfo('name'); ?>
 - 
<?php bloginfo('description'); ?>" />
</a>
<!-- End Logo -->

I don't think I can nest PHP opening and closing tags, so there's that. But, I feel like I need to... my code doesn't work. Can anyone point me in direction of what to try?

Was it helpful?

Solution

I'm not 100% sure on wordpress standards, but here's a way to do it.

$logo1 = "logo1.jpg";
$logo2 = "logo2.jpg";

if ($ti_option['site_logo'] != '') {
    if (is_page(array( 1168, 1433, 1428))){
        $site_logo = $logo1;
    }else if (is_page(array( 1369, 1361, 1365))){
        $site_logo = $logo2;
    }else{
        $site_logo = $ti_option['site_logo'];
    }
?>

} else {

I see 2 issues with your code :

1- No matter the outcome, you end up doing this : $site_logo = $ti_option['site_logo'];

In other words, you never actually change the logo.

2- Might be due to the fact you're using the dots if(cond): instead of if(cond){} which I'm not used to work with but imo it's much clearer when you have a good indentation.

error that is probably making your code crash :

$site_logo = $ti_option['site_logo'];));

should be

$site_logo = $ti_option['site_logo'];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top