Tagged: Sidebar Menu
Hello,
I would like to add “count” of posts under sub categories (in parenthesis) to a nested sidebar menu like;
-Category
– Sub Category A (5)
– Sub Category B (3)
I had found a code by your moderator (“DUDE”) to modify nested sidebar menu and added into my functions.php ;
(I’m not sure if it’s necessary)
add_filter('avf_sidebar_menu_filter', 'avia_add_parent_page_to_sidebar', 10, 3);
function avia_add_parent_page_to_sidebar($sidebar_menu, $args, $post)
{
$parent = $post->ID;
if(!empty($post->post_parent))
{
if(isset($post->ancestors)) $ancestors = $post->ancestors;
if(!isset($ancestors)) $ancestors = get_post_ancestors($post->ID);
$root = count($ancestors)-1;
$parent = $ancestors[$root];
}
$current = ($post->ID == $parent) ? "current_page_item" : "";
$parent_page = '<li class="page_item page-item-'.$parent.' ancestor_page_item '.$current.'">';
$parent_page .= '<a href="'.get_permalink($parent).'">'.get_the_title($parent).'</a>';
$parent_page .= '</li>';
$sidebar_menu = str_replace("<ul class='nested_nav'>", "<ul class='nested_nav'>{$parent_page}", $sidebar_menu);
return $sidebar_menu;
}
Thank you
It would be much more useful as custom widget I guess for other users as well, looking for a solution from moderators and developers.
Thank you,
Best Regards.
Please someone answer
Still no answer?
Hi,
Can you post the link to your website? there’s a native WP function that let you do this:
https://codex.wordpress.org/Function_Reference/wp_count_terms
Regards,
Josue