admin管理员组文章数量:1130349
I have a category named: This & That
However, for some strange reason my switch code doesn't pick it up. When I output $firstcat it does return This & That which makes it even more cumbersome.
The code works fine for other categories not containing the ampersand &.
function posend_text_shortcode() {
$mycategory = get_the_category();
$firstcat = $mycategory[0]->name;
switch($firstcat){
case "This & That":
include(get_stylesheet_directory() . '/inc/style/check.php');
break;
default:
include(get_stylesheet_directory() . '/inc/style/default.php');
break;
}
}
I have a category named: This & That
However, for some strange reason my switch code doesn't pick it up. When I output $firstcat it does return This & That which makes it even more cumbersome.
The code works fine for other categories not containing the ampersand &.
function posend_text_shortcode() {
$mycategory = get_the_category();
$firstcat = $mycategory[0]->name;
switch($firstcat){
case "This & That":
include(get_stylesheet_directory() . '/inc/style/check.php');
break;
default:
include(get_stylesheet_directory() . '/inc/style/default.php');
break;
}
}
Share
Improve this question
asked Jan 6, 2019 at 19:43
JoaMikaJoaMika
6986 gold badges27 silver badges58 bronze badges
5
- And why don’t you check by term slug? It would be much safer... – Krzysiek Dróżdż Commented Jan 6, 2019 at 19:50
- how could I do this please? I am writing mycategory[0]->slug - but I still get the same problem with this category. It works for single words just fine.. – JoaMika Commented Jan 6, 2019 at 20:23
- ok sorry, everything seems to work fine now when I test for slug. thanks for pointing it out – JoaMika Commented Jan 6, 2019 at 20:29
- Take a look at my answer. There are few problems with your code, that I’ve fixed already. – Krzysiek Dróżdż Commented Jan 6, 2019 at 20:33
- Shortcodes are supposed to return their content, not display it – Tom J Nowell ♦ Commented Jan 6, 2019 at 21:20
1 Answer
Reset to default 2Using titles in such comparisons is always a little bit risky - you have to deal with encodings and so on.
Much safer way is using of slugs in code, because they are url-safe.
So your code could look like this:
function posend_text_shortcode() {
$mycategory = get_the_category();
$slug = '';
if ( ! empty($mycategory) ) { // you have to check, if any category is assigned
$slug = $mycategory[0]->slug;
}
switch($slug){
case 'this-that': // change to real slug
get_template_part('/inc/style/check.php'); // you should use get_template_part instead of including template parts
break;
default:
get_template_part('/inc/style/default.php');
break;
}
}
I have a category named: This & That
However, for some strange reason my switch code doesn't pick it up. When I output $firstcat it does return This & That which makes it even more cumbersome.
The code works fine for other categories not containing the ampersand &.
function posend_text_shortcode() {
$mycategory = get_the_category();
$firstcat = $mycategory[0]->name;
switch($firstcat){
case "This & That":
include(get_stylesheet_directory() . '/inc/style/check.php');
break;
default:
include(get_stylesheet_directory() . '/inc/style/default.php');
break;
}
}
I have a category named: This & That
However, for some strange reason my switch code doesn't pick it up. When I output $firstcat it does return This & That which makes it even more cumbersome.
The code works fine for other categories not containing the ampersand &.
function posend_text_shortcode() {
$mycategory = get_the_category();
$firstcat = $mycategory[0]->name;
switch($firstcat){
case "This & That":
include(get_stylesheet_directory() . '/inc/style/check.php');
break;
default:
include(get_stylesheet_directory() . '/inc/style/default.php');
break;
}
}
Share
Improve this question
asked Jan 6, 2019 at 19:43
JoaMikaJoaMika
6986 gold badges27 silver badges58 bronze badges
5
- And why don’t you check by term slug? It would be much safer... – Krzysiek Dróżdż Commented Jan 6, 2019 at 19:50
- how could I do this please? I am writing mycategory[0]->slug - but I still get the same problem with this category. It works for single words just fine.. – JoaMika Commented Jan 6, 2019 at 20:23
- ok sorry, everything seems to work fine now when I test for slug. thanks for pointing it out – JoaMika Commented Jan 6, 2019 at 20:29
- Take a look at my answer. There are few problems with your code, that I’ve fixed already. – Krzysiek Dróżdż Commented Jan 6, 2019 at 20:33
- Shortcodes are supposed to return their content, not display it – Tom J Nowell ♦ Commented Jan 6, 2019 at 21:20
1 Answer
Reset to default 2Using titles in such comparisons is always a little bit risky - you have to deal with encodings and so on.
Much safer way is using of slugs in code, because they are url-safe.
So your code could look like this:
function posend_text_shortcode() {
$mycategory = get_the_category();
$slug = '';
if ( ! empty($mycategory) ) { // you have to check, if any category is assigned
$slug = $mycategory[0]->slug;
}
switch($slug){
case 'this-that': // change to real slug
get_template_part('/inc/style/check.php'); // you should use get_template_part instead of including template parts
break;
default:
get_template_part('/inc/style/default.php');
break;
}
}
本文标签: functionsReturn category name with amp Ampersand doesnt work
版权声明:本文标题:functions - Return category name with & Ampersand doesnt work 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749038318a2306734.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论