admin管理员组文章数量:1332383
I am using the meta tag to define browser bar color on Android and Windows phone. I want to set the theme-color based on category. This is placed in the <head>
section inside my header.php file.
<?php
$post_category = get_the_category()['0']->term_id;
if ($post_category == 75) {
echo "<meta name="theme-color" content="#B7E0D2">";
}
else {
echo "<meta name="theme-color" content="#000000">";
}
?>
But I am getting the white screen of death.
Once I can get that much working I have may have some other issues
- I want to make sure whether they are posts, pages, or custom posts that this works
- There are actually about 12 categories. Should I just have a series of else if statements, or is there a more elegant way of doing this?
I am using the meta tag to define browser bar color on Android and Windows phone. I want to set the theme-color based on category. This is placed in the <head>
section inside my header.php file.
<?php
$post_category = get_the_category()['0']->term_id;
if ($post_category == 75) {
echo "<meta name="theme-color" content="#B7E0D2">";
}
else {
echo "<meta name="theme-color" content="#000000">";
}
?>
But I am getting the white screen of death.
Once I can get that much working I have may have some other issues
- I want to make sure whether they are posts, pages, or custom posts that this works
- There are actually about 12 categories. Should I just have a series of else if statements, or is there a more elegant way of doing this?
1 Answer
Reset to default 1The parser has the problem with usage of "
, because it is not possible what is for PHP or for the syntax to echo.
So change them like.
$post_category = get_the_category()['0']->term_id;
if ($post_category == 75) {
echo '<meta name="theme-color" content="#B7E0D2">';
} else {
echo'<meta name="theme-color" content="#000000">';
}
In the context of your hint to see only the White Screen, read about debugging in WordPress. If you active them you get information about the problem and that helps you to solve them easier.
To your additional question about statement for the category check. If you have always a different color (string) so works that. Maybe you should use the switch
/case
possibility. It is more readable.
You should also work in strict mode. You verify an integer value. So set this as type, like $post_category = (int) get_the_category()['0']->term_id;
. Also, the strict mode for the comparison ===
instead of ==
.
本文标签: categoriesAdd themecolor meta tag to head
版权声明:本文标题:categories - Add theme-color meta tag to head? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742303301a2449419.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论