PHP/Wordpress 查看 post 是否在类别中,然后显示图标。部分工作
PHP/Wordpress Check to see if post is in category, and then display icon. Partially Working
http://104.152.110.248/~baron/category/recipes/ - 我正在处理的网站
所以我正在做的是,当 post(收件人)属于某个类别时,我希望相应的图标出现在 post 的标题旁边.
例如,蟹饼属于鱼类别,因此它应该只在标题旁边显示鱼图标。而 Filet Mingion 只在牛肉类别中,所以它应该只显示牛图标。
我遇到的问题是,如果有任何 post 具有指定的牛肉类别,它会为所有 post 显示一个牛肉图标。虽然我希望它只显示分配给牛肉类别的 post。
这是我目前使用的代码。
<?php
if ($all_the_tags);
$all_the_tags = get_categories();
foreach($all_the_tags as $this_tag) {
if ($this_tag->name == "Spicy" ) {
?>
<img class="icons" src="http://104.152.110.248/~baron/wp-content/themes/Baron/img/icons/Chili Pepper Filled.png">
<?php } else if ($this_tag->name == "Chicken" ) { ?>
<img class="icons" src="http://104.152.110.248/~baron/wp-content/themes/Baron/img/icons/Chicken Filled.png">
<?php } else if ($this_tag->name == "Vegetables" ) { ?>
<img class="icons" src="http://104.152.110.248/~baron/wp-content/themes/Baron/img/icons/Tomato Filled.png">
<?php } else if ($this_tag->name == "Fish" ) { ?>
<img class="icons" src="http://104.152.110.248/~baron/wp-content/themes/Baron/img/icons/Fish Food Filled.png">
<?php } else if ($this_tag->name == "Pork" ) { ?>
<img class="icons" src="http://104.152.110.248/~baron/wp-content/themes/Baron/img/icons/Pig.png">
<?php } else if ($this_tag->name == "Beef" ) { ?>
<img class="icons" src="http://104.152.110.248/~baron/wp-content/themes/Baron/img/icons/Cow Filled.png">
<?php } else {
// it's neither, do nothing
?>
<!-- not tagged as one or the other -->
<?
}
}
?>
试试这个,用下面的代码替换你的代码:
<?php
if (has_category("Spicy",get_the_id())) {
?>
<img class="icons" src="http://104.152.110.248/~baron/wp-content/themes/Baron/img/icons/Chili Pepper Filled.png">
<?php } else if (has_category("Chicken",get_the_id())) { ?>
<img class="icons" src="http://104.152.110.248/~baron/wp-content/themes/Baron/img/icons/Chicken Filled.png">
<?php } else if (has_category("Vegetables",get_the_id())) { ?>
<img class="icons" src="http://104.152.110.248/~baron/wp-content/themes/Baron/img/icons/Tomato Filled.png">
<?php } else if (has_category("Fish",get_the_id())) { ?>
<img class="icons" src="http://104.152.110.248/~baron/wp-content/themes/Baron/img/icons/Fish Food Filled.png">
<?php } else if (has_category("Pork",get_the_id())) { ?>
<img class="icons" src="http://104.152.110.248/~baron/wp-content/themes/Baron/img/icons/Pig.png">
<?php } else if (has_category("Beef",get_the_id())) { ?>
<img class="icons" src="http://104.152.110.248/~baron/wp-content/themes/Baron/img/icons/Cow Filled.png">
<?php } else {
// it's neither, do nothing
?>
<!-- not tagged as one or the other -->
<?
}
?>
参考:https://developer.wordpress.org/reference/functions/has_category/
http://104.152.110.248/~baron/category/recipes/ - 我正在处理的网站
所以我正在做的是,当 post(收件人)属于某个类别时,我希望相应的图标出现在 post 的标题旁边. 例如,蟹饼属于鱼类别,因此它应该只在标题旁边显示鱼图标。而 Filet Mingion 只在牛肉类别中,所以它应该只显示牛图标。
我遇到的问题是,如果有任何 post 具有指定的牛肉类别,它会为所有 post 显示一个牛肉图标。虽然我希望它只显示分配给牛肉类别的 post。
这是我目前使用的代码。
<?php
if ($all_the_tags);
$all_the_tags = get_categories();
foreach($all_the_tags as $this_tag) {
if ($this_tag->name == "Spicy" ) {
?>
<img class="icons" src="http://104.152.110.248/~baron/wp-content/themes/Baron/img/icons/Chili Pepper Filled.png">
<?php } else if ($this_tag->name == "Chicken" ) { ?>
<img class="icons" src="http://104.152.110.248/~baron/wp-content/themes/Baron/img/icons/Chicken Filled.png">
<?php } else if ($this_tag->name == "Vegetables" ) { ?>
<img class="icons" src="http://104.152.110.248/~baron/wp-content/themes/Baron/img/icons/Tomato Filled.png">
<?php } else if ($this_tag->name == "Fish" ) { ?>
<img class="icons" src="http://104.152.110.248/~baron/wp-content/themes/Baron/img/icons/Fish Food Filled.png">
<?php } else if ($this_tag->name == "Pork" ) { ?>
<img class="icons" src="http://104.152.110.248/~baron/wp-content/themes/Baron/img/icons/Pig.png">
<?php } else if ($this_tag->name == "Beef" ) { ?>
<img class="icons" src="http://104.152.110.248/~baron/wp-content/themes/Baron/img/icons/Cow Filled.png">
<?php } else {
// it's neither, do nothing
?>
<!-- not tagged as one or the other -->
<?
}
}
?>
试试这个,用下面的代码替换你的代码:
<?php
if (has_category("Spicy",get_the_id())) {
?>
<img class="icons" src="http://104.152.110.248/~baron/wp-content/themes/Baron/img/icons/Chili Pepper Filled.png">
<?php } else if (has_category("Chicken",get_the_id())) { ?>
<img class="icons" src="http://104.152.110.248/~baron/wp-content/themes/Baron/img/icons/Chicken Filled.png">
<?php } else if (has_category("Vegetables",get_the_id())) { ?>
<img class="icons" src="http://104.152.110.248/~baron/wp-content/themes/Baron/img/icons/Tomato Filled.png">
<?php } else if (has_category("Fish",get_the_id())) { ?>
<img class="icons" src="http://104.152.110.248/~baron/wp-content/themes/Baron/img/icons/Fish Food Filled.png">
<?php } else if (has_category("Pork",get_the_id())) { ?>
<img class="icons" src="http://104.152.110.248/~baron/wp-content/themes/Baron/img/icons/Pig.png">
<?php } else if (has_category("Beef",get_the_id())) { ?>
<img class="icons" src="http://104.152.110.248/~baron/wp-content/themes/Baron/img/icons/Cow Filled.png">
<?php } else {
// it's neither, do nothing
?>
<!-- not tagged as one or the other -->
<?
}
?>
参考:https://developer.wordpress.org/reference/functions/has_category/