0

I'm trying to change the logo depending of the page I'm browsing. The goal is to have different logo style on all single product page.

What I have in my function.php so far is this. I've tried to check by is_product() but the logo isn't changed.

function change_logo_on_single($html) {

   if(is_product()){
      $html = preg_replace('/<img(.*?)\/>/', '<img src="Black.png" class="custom-logo" alt=logo"" itemprop="logo" />', $html);
   }

   return $html;
}

add_filter('get_custom_logo','change_logo_on_single');
S.I.
  • 3,250
  • 12
  • 48
  • 77

3 Answers3

1

You can use filter as below:

function change_logo_on_single( $html ) {

  if ( is_product() )
    return '<img src="Black.png" class="custom-logo" alt=logo"" itemprop="logo" />';

  return $html;
}
add_filter( 'get_custom_logo', 'change_logo_on_single', 10, 3 );
  • Thanks but doesn't work. The problem must be in `get_custom_logo` because I'm not sure if this is used in the theme to call the logo and I'm trying to find how it is done. – S.I. Jan 31 '19 at 09:57
1

You can make an custom action for this

<?php 
add_action('my_theme_logo', function(){
   if ( is_product() )
    return '<img src="singlelogo.png" alt="single-logo" />';
   else
    return '<img src="main-logo.png" alt=logo"" />';
});
?>

and use action in your theme as below defined

<?php 
 echo do_action("my_theme_logo");
?>

0

Isn't it something rather on the browser's side than on server's? You could use javascript to change the source of your image. How to do it is described here.

Pati K
  • 129
  • 2
  • 11