How to display specific categories

Posted in: Newspaper
Post count: 42

Hello,

I would like to display certain specified categories on a page (for example, Archive). And I want those categories to be active (visible) only if they have posts. For example, January, February, March, April (here in April, we don’t have any posts yet… when there are posts in this category, “April” should appear). Is this possible?

Thank you in advance

Post count: 21065

Hello @ebhelloworld28!

Unfortunately, there is no function that prevents the template from being displayed if the posts have not yet been created. 

Thank you for understanding!

Post count: 42

Hello,

I found a solution. You can do it using this code. I have created a menu with categories :).

add_filter( ‘wp_nav_menu_objects’, ‘hide_empty_categories_from_menu’, 10, 2 );
function hide_empty_categories_from_menu( $items, $args ) {
foreach ( $items as $key => $item ) {
if ( $item->object == ‘category’ ) {
$category = get_category( $item->object_id );
if ( $category->count == 0 ) {
unset( $items[ $key ] );
}
}
}
return $items;
}

Viewing 3 posts - 1 through 3 (of 3 total)
The forum ‘Newspaper’ is closed to new topics and replies.