Different favicon on different pages

Is it possible to have different favicon on on different pages?

For example:

mywebsite.com/blog-1 - favicon-1
mywebsite.com/blog   - favicon-2

And so on.

I have custom domain name for each blog (And I don't want to put the same favicon on these pages)

Topic favicon functions wp-head customization Wordpress

Category Web


As of WordPress version 5.6, any posts or pages has options on the editor to include any custom scripts/styles and meta tags to the header of the page. So if you put your <link rel"icon" href="favicon.png"/> it should work.

You upload your favicon into the media and after you uploaded the media. Refer the uploaded favicon png image reference directly to the link.

Example of favicon in header block

<link rel="icon" href="https://domainname.com/uploads/2020/10/10/favicon-32x32.png" />

This might be overkill, but if you are looking to swap out favicons for specific pages, post archives, or even single posts, here is something that should get you there. Hopefully it's clean enough to maintain. And you can drop in whatever sort of is_ conditional WordPress has to offer.

 <?php
  switch(true){
    case is_page(27) :
      $favicon_link = 'link_to_favicon_one.png';
      break;
    case is_page(array(23, 40, 44, 60)) :
      $favicon_link = 'link_to_favicon_two.png';
      break;
    case is_post_type_archive() :
      $favicon_link = 'link_to_favicon_three.png';
      break;
    case is_single(2001) :
      $favicon_link = 'link_to_favicon_four.png';
      break;
    default : // Always need a fallback
      $favicon_link = 'link_to_favicon.png';
      break;
  }
?>

<link rel="icon" href="<?=$favicon_link?>" sizes="32x32" /> 

Hope this helps someone!


You can use a few solutions;

  1. Create meta-field with favicon in your page/post. And call this in your header.php file. But dont forget set default value, if meta-field will be empty.
  2. Wrap this meta tags in apply_filter and create filters for this.
  3. Use conditionals like is_page(), is_singular('product') etc..
  4. Remove meta tags from header.php and use add_action('wp_head', 'your_functions_tags');
  5. Create header-{slug}.php. header-fav1.php, header-fav2.php and call in your template like <?php get_header('fav1'); ?>

Something for this list must help you.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.