I am trying to create a shortcode to display the posts count in a category.
I have successfully done this using this code:
// Add Shortcode to show posts count inside a category
function add_count_of_posts_in_category() {
$term = get_term( 7, 'category' );
$count = $term->count;
echo $count;
}
add_shortcode( 'show-posts-count', 'add_count_of_posts_in_category' );
However, this means that I need to specify the ID of the category for the shortcode to work. Which means that i need to create a shortcode per category, which is useless.
I am trying to find a way to modify the category ID part with a variable, so that I can use the shortcode like this:
[show-posts-count="cars"] to display the post count in the category called cars.
I can’t find a way to do so.
Your help is much appreciated.
EDIT: 29/09/2016
After getting the code working, I am trying to expand the function to count the posts in the child category too.
So if the main category does not have posts, but has 2 subcategories, each has posts, then when I use the shortcode on the main category, the displayed number is the sum of all the posts in the main category (if any), in addition to, the number of posts in the sub-categories, and the sub-sub-categories..etc
I tried using get_term_children( $term, $taxonomy );, but did not know how to get the post count of subcategories then add them together.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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');
Method 2
To count posts in subcategories in addition to specified category, one possibility is to query posts using a cat option and count the results. cat option queries posts in child categories by default.
add_shortcode('category_post_count', function ($atts, $content) {
$atts = shortcode_atts([
'category' => 0
], $atts);
$cat_id = is_numeric($atts['category']) ?
intval($atts['category']) :
get_category_by_slug($atts['category'])->term_id;
return count(get_posts([
'nopaging' => true,
'fields' => 'ids',
'cat' => $cat_id
]));
});
There are examples of querying the child categories and summing up the count fields, but this may yield invalid results if a post belongs to more than one of the matched categories.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0