Every time you step through the outermost foreach()
loop, your $total_count
gets reset.
Try this:
$categories = get_categories([
'hide_empty' => 0,
'parent' => 0,
]);
$total_count = 0;
foreach ($categories as $category) {
$subcategories = get_categories([
'hide_empty' => 0,
'parent' => $category->term_id,
]);
$total_count += $category->count; // Adds the main category count
foreach ($subcategories as $subcategory) {
$total_count += $subcategory->count; // Add subcategory counts
}
echo '<input type="checkbox" data-select="sectionFilter" id="section_' . $category->term_id . '" value="' . $category->term_id . '">';
echo '<label for="section_' . $category->term_id . '">';
}
Related Posts:
- How to show related posts by category
- Including categories in search results
- Count how many posts in category
- Custom Single Post By Category
- Convert custom fields to post categories
- How to disable Uncategorized category URL?
- Add role that restricts user to post in specific category
- Query posts from current year
- Show One Category on Post
- Display all posts under child sub category in sidebar on post page?
- Do I use custom post type or something else?
- What Defines What Category A Post Picks (if in multiple)
- Limit function to specific post category
- Authors in menu, template list post by author
- Show all post titles
- Add Category name to Post Title (h1)
- Posts are not showing up on particular category
- Count to how many categories a post belongs to
- How to display related posts by subcategory and not parent category
- Category Template: Need to display different content on first page of archives
- Display post category in foreach loop with category link
- Get mixed category random posts
- Extracting post categories
- Using wp_list_categories like the the_category (showing just current categories of a post)
- How to limit posts by category
- How to allow users to post only in certain category and hide elements from edit page?
- Get user categories with most posts in it
- Need help with category listing!
- Prepending %category% onto default posts fails
- How to list recent posts in a wp nav menu?
- Dynamic dependent Dropdown lists for categories, sub-categories and posts
- How to display two blog categories as separate sections on one page?
- How to make the first post in the loop be styled like a “new / featured” post?
- why does wordpress ignore the post args?
- How can you display all sibling categories to a post?
- get posts from Custom Post Type & Category
- How to create a “latest news” page showing a list of posts from blog category
- how to hide empty fields of post category description?
- How to enable custom Shortcodes in Post Category Description? [duplicate]
- Sort by last word in title
- How to call posts under a specific category on static front page?
- Give posts a particular template based on the assigned category
- All categories displays the same posts
- Having pages after specifying post_type post
- Get latest post from categories
- Post-Archive like a page in a specific subdirectory
- How to show a featured image inside a post in only 1 single category
- How to display only one category in a custom post type?
- Get only X number of categories
- inserting a category into post
- Getting posts from some categories plus some individual posts
- Change layout of post depending on category
- latest post showing up twice on posts page
- wp_list_categories() Exclude All Categories Except One
- Geting error in post and category [closed]
- Different post views for different category views
- Display most recent posts in category with Twenty Ten theme widget
- Possible?! A contextually titled back link
- Allow / show post comments in category pages
- How to select a category automatically based on a word in WordPress post title?
- Display default matabox of posts(add category) wordpress
- How display related post only in Default category on single
- How to get tags when using publish_post
- How to add class to specific navbar item when post parent category is in specific category
- How do you change the permalink for posts for a single category?
- Posts are not displaying on their category and tag page
- Does having category name in permalinks affect SEO when having a post in multiple categories?
- how to create a category with wp_insert_post and post_category
- How to 301 redirect from url with post id to permalink with post name (slug)?
- Create/Set Category as Title of Post
- Exclude posts in a category on one page but show those posts on a different page
- How to add a block to a category page?
- Excluding category from post navigation in WordPress?
- Post Category link is same with Page link
- Get 5 most recent categories
- Show custom post category for single post
- Get a list of categories that are related to posts
- Querying posts from current category, using a variable as array argument
- Precedence of page permalinks over woocommerce product category links?
- Need help writing loop to display posts by categories in separate divs
- Add category selection to function request
- Query post category & remove any post id
- Admin Post List Only Show One Category
- Multiple Post Repeat for Related Post Loop by Category
- Show Post number of specific Category
- Add category attribute to custom recent post shortcode
- Retrieve latest post by multiple categories with ID
- how to show posts on different pages if number of post per page is one
- Is possible to create a custom link that creates a post that’s already categorized?
- Is it possible to have different header style blog titles in different categories?
- Category pages vs single post pages
- How to display subcategory articles in the category view?
- How do we display a certain category type on a page while hiding all others?
- Show Title/Date/Excerpt of first post & only Title for rest in Query [duplicate]
- Show some texts in posts belonging to Specific category
- wordpress category Description not allowed tag
- Sort Posts Alphabetically Based on Specific Category (Divi)
- I have a website issue I am trying to resolve
- How to sort posts alphabetically based on a specific parent category
- How to get a list of all posts and their categories?