admin管理员组文章数量:1130349
I have two categories for my posts, "event" and "long-term-leasing." I have sidebar-events.php and sidebar-long-term.php. I wrote a conditional statement in single.php, but when I view a post it only shows me the generic sidebar. I did a copy/paste of the categories to avoid typos. Where is my mistake?
<?php
if (is_category("event")) {
get_sidebar('events');
} elseif (is_category('long-term-leasing')) {
get_sidebar('long-term');
} else {
get_sidebar();
}
?>
I have two categories for my posts, "event" and "long-term-leasing." I have sidebar-events.php and sidebar-long-term.php. I wrote a conditional statement in single.php, but when I view a post it only shows me the generic sidebar. I did a copy/paste of the categories to avoid typos. Where is my mistake?
<?php
if (is_category("event")) {
get_sidebar('events');
} elseif (is_category('long-term-leasing')) {
get_sidebar('long-term');
} else {
get_sidebar();
}
?>
Share
Improve this question
asked Dec 22, 2018 at 23:50
marilynnmarilynn
51 bronze badge
6
|
Show 1 more comment
2 Answers
Reset to default 0Try has_category, instead; is_category is used for archive pages, not single posts.
For all who are struggling, here is what worked for me:
<?php
if (in_category("event")) {
get_sidebar('events');
} elseif (in_category('long-term-leasing')) {
get_sidebar('long-term');
} else {
get_sidebar();
}
?>
I have two categories for my posts, "event" and "long-term-leasing." I have sidebar-events.php and sidebar-long-term.php. I wrote a conditional statement in single.php, but when I view a post it only shows me the generic sidebar. I did a copy/paste of the categories to avoid typos. Where is my mistake?
<?php
if (is_category("event")) {
get_sidebar('events');
} elseif (is_category('long-term-leasing')) {
get_sidebar('long-term');
} else {
get_sidebar();
}
?>
I have two categories for my posts, "event" and "long-term-leasing." I have sidebar-events.php and sidebar-long-term.php. I wrote a conditional statement in single.php, but when I view a post it only shows me the generic sidebar. I did a copy/paste of the categories to avoid typos. Where is my mistake?
<?php
if (is_category("event")) {
get_sidebar('events');
} elseif (is_category('long-term-leasing')) {
get_sidebar('long-term');
} else {
get_sidebar();
}
?>
Share
Improve this question
asked Dec 22, 2018 at 23:50
marilynnmarilynn
51 bronze badge
6
-
Are you sure they are terms in the category taxonomy? If so, maybe you need to reset the main WordPress query - add
wp_reset_query();before theifblock starts. – Sally CJ Commented Dec 23, 2018 at 0:14 - They are categories. I cut and pasted from the category dashboard to ensure accuracy. I tried your suggestion, but sadly there was no change. – marilynn Commented Dec 23, 2018 at 3:30
-
Ah, "in
single.php". That's why theis_category()fails. Tryin_category(). But I assume the post would only be in one of those categories? – Sally CJ Commented Dec 23, 2018 at 5:11 - 1 in_category() worked! That one letter change just salvaged my weekend. Sally CJ, I shall also worship you. – marilynn Commented Dec 23, 2018 at 6:14
- I would like to post the corrected code here, but I can't figure out how to properly wrap the code. – marilynn Commented Dec 23, 2018 at 6:29
2 Answers
Reset to default 0Try has_category, instead; is_category is used for archive pages, not single posts.
For all who are struggling, here is what worked for me:
<?php
if (in_category("event")) {
get_sidebar('events');
} elseif (in_category('long-term-leasing')) {
get_sidebar('long-term');
} else {
get_sidebar();
}
?>
本文标签: categoriesSidebar by Category Conditional Statement not functioning
版权声明:本文标题:categories - Sidebar by Category Conditional Statement not functioning 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749074255a2312056.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


wp_reset_query();before theifblock starts. – Sally CJ Commented Dec 23, 2018 at 0:14single.php". That's why theis_category()fails. Tryin_category(). But I assume the post would only be in one of those categories? – Sally CJ Commented Dec 23, 2018 at 5:11