There is no coding involved here. WordPress has already a build in widget for this function. If you go to the “Widgets” screen. you’ll see that there are a widget called Categories

You can drag and drop that to your sidebar where you need to display the widget. When you’ve done that, the widget will open, from there you can tick the ‘Show post counts’ box to display the post count of the categories.

Related Posts:
- How to use in_category?
- How to add category to: ‘wp-admin/post-new.php’?
- Use category base slug in posts’ permalink
- Setting Custom Sort Order of Posts within a Category
- Display the latest post from a category in a page
- How to show list of posts by author and category?
- Exclude posts that only have the ‘Uncategorized’ category [duplicate]
- Getting Position of a post in a category
- Display only posts from a specific parent category
- How to control which category will be picked for the slug of a post?
- Add default content to post (for specific category)
- Query only Posts from Both of Two Category?
- Display most recent post in category instead of archive?
- Post in Multiple Categories to stay in current category (permalink, next previous post link)
- get_post_type_archive_link(‘post’) returns current category archive
- Get user’s most published categories
- Conditional that checks if a current category has any subcategory with posts
- How to set post expiration date and time and move the page to archive after expiration [closed]
- Exclude posts from featuring
- Articles are text-only in any “Category” instead of HTML
- How To Remove Certain category Posts From the Blog Page
- Display posts in three columns by category
- Add icon/badge next to posttitle of specific category
- Deleted category by mistake, all posts now have categorized category. Can i revert that?
- Get current category id from post page wordpress
- Ajax load more button in Recent posts widget
- Saving category to a post, before publishing the post
- Assign category to new post via URL
- Stop users posting into category while still seeing contents of the category
- Category Page Template not Showing Posts
- How to Restrict Previous & Next Post Link to Posts of Same Category?
- WordPress custom Query for Posts in Category display posts multiple times
- Categories list into registration form
- Using ajax on editing a category edit page?
- Disable sticky option for specific categories
- date issue with category post retrival
- How to make multiple sections in home pulling posts category wise?
- Excluding posts not working
- How to get subcategories from category slug?
- Showing categories and subcategories with posts
- Change default category when I publish a post
- Loop doesn’t exclude the specified category in home page
- How do I show posts from another wordpress installation?
- How do I include the category next to the title of a post?
- How to show a custom taxonomy in the theme?
- How to import nested categories from XML file?
- Display a mixed list of posts and media
- Migrate posts from category and sub-category via SQL
- Posts Missing in Dashboard after update
- Display post number by category
- Custom posts listing widget always returns 1 extra result
- How to get post with slug and exclude categories
- Retrieving posts by their date and category
- Are post, page and category IDs unique to each other?
- Static pages not working
- WordPress displays post on subcategory only
- Sidebar limiting to 10 posts?
- Categories Listing and Highlighting current category item
- Display hierarchical categories/sub-categories for a single post only
- Post in bulk based on category template
- Automatic duplicate/copy of post to a related page in WordPress
- Custom Fields – How to get the list of a specific active widget each time it rendered
- Yoast breadcrumb URL shows category instead of the page
- Author post count in category
- Query prints posts without specific categories
- Foreach issue in get_posts function
- Why do I have categories with duplicate slugs?
- Automatic Table of contents with categories and posts
- post categories – how to show only categories with a specific parent id
- Creating a “category” of featured posts
- Posts on Sidebar only
- Is it possible to remove actual post pages in wordpress but keep them in categories
- How do I make it so that new posts within a certain category go into a certain page?
- Need help with hiding an image within a post in a category
- WordPress not showing recent posts
- How to get post count including nested categories
- List Authors For Current Category
- How to filter out post from a category not its subcategory in wordpress dashboard
- How can I display a specific number of post in a category via a url
- how to chang preview of list of articles of one category?
- Post of a specific category – not to be seen in paging previous/next post
- How to show main category related to that post
- Display selected category on post page
- Assign specific ads to specific categories/posts
- Sort post by category using HTML Select tag
- Make assigning post to a specific category equivalent to assigning it to all categories
- About number of posts in selected category
- Add default content to post (for specific category)
- List posts that are in the same category as the current post title
- Category page shows “No Posts” but I do have two assigned
- How to have more post in a page than in your home page
- Exclude a ‘portfolio’ custom category?
- Multiple blog “feeds” in a single blog each with distinctly styled post pages
- How to get pagination working with “Pending” Posts
- ‘get_previous_post’ in same category returning (!empty) when no previous post in category exists
- Post’s arent displaying
- Show with echo if a post has two or more categories
- excerpt in template for specific page
- Only show posts belonging to multiple cateogries at same time
- Wrap posts from a category in HTML