admin管理员组文章数量:1128713
Is it possible to have different favicon on on different pages?
For example:
mywebsite/blog-1 -> favicon-1
mywebsite/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)
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)
Share Improve this question edited Mar 24, 2020 at 20:54 WordPress Speed 2,2833 gold badges19 silver badges33 bronze badges asked Feb 5, 2018 at 12:25 CharboterCharboter 11 gold badge1 silver badge3 bronze badges 4 |4 Answers
Reset to default 1As 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" />
You can use a few solutions;
- 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. - Wrap this meta tags in
apply_filter
and create filters for this. - Use conditionals like
is_page()
,is_singular('product')
etc.. - Remove meta tags from
header.php
and useadd_action('wp_head', 'your_functions_tags');
- 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.
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!
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.
本文标签: functionsDifferent favicon on different pages
版权声明:本文标题:functions - Different favicon on different pages 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736698960a1948326.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
is_single(123)
that is only true if the post with the ID 123 is currently shown to fine tune what to show. – janh Commented Feb 5, 2018 at 13:00