0

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)

3
  • Why not? Just set the appropriate meta tags and see what the browser make of it.
    – janh
    Feb 5, 2018 at 12:29
  • I really don't know where to put that meta tags because I want a specific favicon for a specific page, if I add a meta tag in the template it will be generic, right ?
    – Charboter
    Feb 5, 2018 at 12:53
  • Yes, but you can use conditional tags like is_single(123) that is only true if the post with the ID 123 is currently shown to fine tune what to show.
    – janh
    Feb 5, 2018 at 13:00

4 Answers 4

1

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" />
1
  • Wouldn't it mean that the page would now contain multiple different icons? When I was doing something similar, it was a bit random which of the icons was chosen by the browser, so it wasn't reliable.
    – piit79
    Mar 8 at 15:53
0

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.

5
  • Its very kind and I'm sure that working, but I really can't figure out how to make this working, I never before work with this kind of meta in Wordpress. I tried to read some article about it but it's like "black magic" for me. Anyway, thanks for your time.
    – Charboter
    Feb 5, 2018 at 13:40
  • Read about meta fields. And template files. Easier way - Advaced Custom Fields plugin. Feb 5, 2018 at 13:43
  • Thanks for your advice. I read carefully these articles : codex.wordpress.org/Conditional_Tags pagely.com/blog/find-post-id-wordpress wpbeginner.com/wp-tutorials/… code.tutsplus.com/tutorials/… developer.wordpress.org/themes/basics/template-files Maybe I'm just dumb about this but, I understand each of point you mentioned but I don't understand how to correctly implement this.
    – Charboter
    Feb 5, 2018 at 16:31
  • Okay so I make it worked with this : if ( get_the_ID() == 27) { ?> <link rel="icon" href="mylink" sizes="32x32" /> <?php } ?> But when I look at the source code generated, it displays first my main favicon and just after mine...Is there a way to conditional the favicon display of the main site ?
    – Charboter
    Feb 5, 2018 at 17:31
  • It's important to note that browsers apply heavy caching to favicons - sometimes even changing then site wide can take a while to see the difference - it depends on a lot of variables. Apr 24, 2020 at 15:40
0

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!

0

I just needed to do something like this. I thought modifying <head> directly didn't sound like the best idea, so I looked at the Wordpress source.

I found that the favicon URL is filtered through the get_site_icon_url filter (see https://developer.wordpress.org/reference/functions/get_site_icon_url/). In this filter, you can find out what page you're on (for example using the get_current_screen() function) and then return a different URL.

Here's my code for reference:

function get_custom_favicon( $url, $size, $blog_id ) {
    $favicons = array(
        'edit-product' => 13975, // attachment ID
        'edit-shop_order' => 13974,
    );
    
    $screen = get_current_screen();
    $screen_id = $screen ? $screen->id : 0;

    if ( isset( $favicons[ $screen_id ] ) && ( $image_id = $favicons[ $screen_id ] ) ) {
        if ( $size >= 512 ) {
            $size_data = 'full';
        } else {
            $size_data = array( $size, $size );
        }
        $url = wp_get_attachment_image_url( $image_id, $size_data );
    }

    return $url;
}
add_filter( 'get_site_icon_url', 'get_custom_favicon', 10, 3 );

The array at the top contains a mapping from the page ID to image attachment ID (which you can get easily from the page URL when you open the image Media in Wordpress). The code within the isset() if-statement is lifted directly from the WordPress source, so it handles the favicon image in the exact same way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.