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;
}
}
Using 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;
}
}