I am trying to make a WordPress shortcode that should show a text containing the number of posts in a specific category and if the category is empty it should return a text saying that the category is empty.
The shortcode I have made so far kinda works. The only problem is that it keeps returning 0 posts even though there are numerous posts in that specific category.
I have tried different functions such as is_category() and get_category() but neither works. Does it make any difference that the category is related to a custom post type? The post type slug is projekt btw.
function imbro_aaben_projekt_shortcode() {
$category = get_category('aaben-projekt');
$theCount = $category->count;
if ( $theCount > 0 ){
return 'Total: ' . $theCount . ' posts in this category';
} else {
return 'There are no posts in this category';
}
}
add_shortcode( 'imbro_empty', 'imbro_aaben_projekt_shortcode' );