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
- 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
- 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
- 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
- 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
- How to output a PHP file values by shortcode?
- How to display posts by current user in a drop down
- 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
- Pass php dynamic variable to shortcode
- How to Reference/Echo Variable from Another PHP Function
- Remove echo from shortcode
- 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?
- Creating WordPress Shortcode with Variable
- Shortcode to find and replace URL
- Isotope Drop Down Categories Filter Function
- Help with WordPress function inside a shortcode
- Shortcode to embed Edit Account form not working
- Hierarchical taxonomy list with modificated term links
- Shortcode for Listing Users from Meta Value?
- How to get current post category details inside “loop”?
- Post not populating for custom post type based on category selection
- Need to Modify a WordPress Shortcode with another wordpress shortcode
- How to return a string that has a jQuery and Ajax inside in a shortcode?
- Create special button on WP Tiny MCE Posts Editor for Shortcodes
- AJAX function not working [closed]
- How to change the author box selection on a post to a custom taxonomy
- Counting number of posts in multiple (sub)categories using shortcode
- How to hide a child category and show his parent category
- Display logged in user name and lastname on page
- How do I edit the terms output args or array data?
- Why is the current page loaded in the pop-up window and not the specified one?
- How do I reopen the (Popup Maker) after entering the correct password for a password protected page?
- Block error message in foreach loop when looping through ACF field
- How can I prevent a shortcode div from extending beyond its boundaries?
- PHP multiple forms, same page, isset($_POST[]) not working?
- Get parent category name when only child category is applied to a product
- Gravity Forms: How to add PHP function to confirmation conditional shortcode?
- wpdb->query returns different value to phpMyAdmin
- shortcodes, custom php and their errors
- 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?
- 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
- Customizing the output of the archive and category widget without altering the original behavior of the widget
- Short code into PHP
- PHP get_category() function redeclared
- Need help with PHP functions
- Get category id for a custom category and display it in a class
- Trying to see if page is category or single and displaying title with appropriate heading tag
- Shortcoding with Divs
- ISOTOPE – Missing/Invalid Arguement Get Terms
- category & sub category order issues in wordpress
- Displaying categories
- My title is showing after the 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)
- 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