The shortcode
// Add Shortcode to show posts count inside a category
function category_post_count( $atts ) {
$atts = shortcode_atts( array(
'category' => null
), $atts );
// get the category by slug.
$term = get_term_by( 'slug', $atts['category'], 'category');
return ( isset( $term->count ) ) ? $term->count : 0;
}
add_shortcode( 'category_post_count', 'category_post_count' );
Usage
[category_post_count category="category_slug_or_name"]
If you want to get the count by name, not slug change this
$term = get_term_by( 'slug', $atts['category'], 'category');
to this:
$term = get_term_by( 'name', $atts['category'], 'category');
Related Posts:
- Redirect to another page using contact form 7? [closed]
- Display Visual Composer shortcode if a post belongs in specific categories
- How to manually fix the WordPress gallery code using PHP in functions.php?
- exclude multiple terms using get_terms() function
- Shortcode putting html such as
- Shortcode created to check language not works
- Allow authors to post only in categories they create in WordPress
- Add custom field to attachments in a specific category
- How to Populate the list of custom post type categories
- Php string not working in WordPress Functions.php [duplicate]
- Add/echo div with Analytics-Code to function in functions.php
- Custom plugin issue when trying to use the shortcode twice on a page [closed]
- How can I default to all WordPress roles when parameter is not included in shortcode?
- WordPress menu deletes when trying to add a hook
- Problems with function on function.php
- Use template for posts with a particular category grandparent
- Wrap each shortcode in array to div
- Check if Product is in a Specific Category in Functions.php
- Recent post display using shortcode
- Nested shortCode functions in the functions.php file
- Making Quote Plugin more efficient
- Placing raw HTML inside a WordPress shortcode
- Woocommerce – remove product from category
- Shortcode content output but not in correct place
- is_page “range” for if statement?
- PHP code inside shortcodes
- Shortcode content filter?
- get current product name in functions.php
- Trigger popup in a php if/else statement
- get post based on category chosen in drop down – The ajax method
- Swapping wp_dropdown_categories function with wp_category_checklist
- How to output a PHP file values by shortcode?
- Showing content from one page on another
- How to display posts by current user in a drop down
- Get value from shortcode to do something
- How to exclude category ID from Looper in WordPress
- Add a specific part of current category page url to shortcode
- Pass Category Name, Description and Photo into variables to pass to jQuery
- How to pick the default selected value in wordpress dropdown?
- Get page that displays all children of taxonomy parent
- Custom shortcode outputs plain text instead of HTML at top of post
- Pass php dynamic variable to shortcode
- How to Reference/Echo Variable from Another PHP Function
- Remove echo from shortcode
- register_taxonomy() take much queries
- Shortcode with PHP issue “Undefined index”
- Dilemma of Populating all the categories in a drop down list
- Edit the_category (); for a hierarchical display
- Category title output before opening title tag
- List taxonomy term slugs within shortcode (do_shortcode)
- Shortcode from a function not working
- Display Data in Table from External Database in WP using Shortcodes
- Is it possible to return content, and then also continue to do other things?
- ::before on open/close function [closed]
- Is it possible to define variables in a wordpress shortcode, and then call the shortcode using a specific variable?
- Creating WordPress Shortcode with Variable
- Shortcode to find and replace URL
- Displaying posts based on category
- Display a custom name when the user has no name settle in his account
- Having trouble creating two shortcodes, one for logged in user and one for visitors
- Multiple Tag Filtering
- Notice: Undefined variable
- Hide a specific category title from displaying on site
- Display current user metadata on WordPress page
- Reload page with a different shortcode when a user selects from a dropdown
- How Can I Display the Last Modified Date for User Profiles on WordPress?
- PHP/CSS: Shortcode won’t display correctly, and only displays in the head (before content)
- Offset with ajax load more posts duplicates
- Get posts by id using shortcode
- Shortcode’s output to use as other shortcode’s parameter
- Populate Posts based on category selected using AJAX that means without page refresh
- All categories options or All categories not Populating
- Customizing the output of the archive and category widget without altering the original behavior of the widget
- Open/closed function [closed]
- Need to Echo A Url path to show on a wordpress page
- Short code into PHP
- PHP get_category() function redeclared
- Wrapping shortcode content in a span or link
- Displaying categories
- Whats wrong with my code? Need To add String to shortcode? [closed]
- My title is showing after the shortcode
- A next page function with shortcode?
- Shortcode to include PHP file, pass various parameters to include?
- How to insert the first letter in uppercase [closed]
- Is there a way of displaying related posts from lowest hyrachical order (lowest child category to parent category)
- WP Custom tables query
- Display terms on product page with shotrcode
- Hide child of parent categories already hidden with “get_terms_args” – Woocommerce
- How to extract URLs from wordpress taxonomies
- How call WordPress shortcode inside function file
- Show Login Errors In WordPress/Elementor (Code “works”, but breaks site)
- How to limit number of posts in the certain category and exclude the oldest post automatically
- Exclude a category ID from the following function
- Calling a function via a shortcode in javascript
- Adding a css class to the gallery
- Strip and print only the numbers found in current’s post excerpt (even if they are without space)
- Woocomerce custom add to cart button edit functionality [closed]
- Put ACF function output code the_terms
- Help with get_categories code
- Is it possible to go to the next post in the same category?