• Presson

    (@pressonforlife)


    Hello great devs,

    Please, I need help with finding both PHP Code Snippet, and Shortcode that can help me to Display the Post Count in a Category, anywhere on my Post or Page.

    For example, if my Categories are: Shoes, Dress, and Perfumes, and they have Category ID of 1,2, and 3.

    I want to be able to insert a shortcode like [show_count cat_id=”1″], and it should be able to show the Post count in Category 1, which is Shoes.

    Can anyone Please help with that?

    Thanks.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Did you try using “Categories List” block instead? It has an option to show count.

    Thread Starter Presson

    (@pressonforlife)

    The challenge is that I am developing with Elementor: so I need to place shortcode.

    Moderator threadi

    (@threadi)

    Are you interested in the output of lists of categories (or a taxonomy)? Then this list must already provide such a function. You cannot add anything to it with shortcodes.

    I would therefore recommend that you contact the Elementor support forum: https://ww.wp.xz.cn/support/plugin/elementor/

    Give this a shot …

    php =>

    function CategoryCount($atts){
    $a = shortcode_atts( array( 'category-id' => '555', ), $atts );
    $CategoryID = $a['category-id'];
    $CategoryData = get_category( $CategoryID );
    $CategoryCount = '<p>Category Count: '.esc_html($CategoryData->count).'</p>';
    return $CategoryCount;
    }
    add_shortcode('Category_Count', 'CategoryCount');

    Shortcode =>

    [Category_Count category-id=”42″]

    This should work for the standard post, post type (tested on a dev site).

    If you need something for a custom post type, you need to pass the custom taxonomy. I can fire over a version for custom post type, if needed.

    cheers

    scotty

    Hi @pressonforlife ,

    Please try below code and let me know if you face any issue.

    /**
    * Display post count for a specific category
    */
    function custom_display_category_post_count($atts) {
    // Extract shortcode attributes
    $atts = shortcode_atts(
    array(
    'cat_id' => '', // Category ID
    'text' => 'Total post is: ', // Optional text
    ),
    $atts,
    'show_count'
    );

    // Validate category ID
    if (empty($atts['cat_id']) || !is_numeric($atts['cat_id'])) {
    return '<span class="category-count-error">Please provide a valid category ID</span>';
    }

    // Get category information
    $category = get_category($atts['cat_id']);

    // Check if category exists
    if (!$category) {
    return '<span class="category-count-error">Category not found</span>';
    }

    // Get the post count
    $count = $category->count;

    // Output
    $output = '<span class="category-post-count category-' . esc_attr($atts['cat_id']) . '">';
    $output .= esc_html($atts['text']) . number_format_i18n($count);
    $output .= '</span>';

    return $output;
    }
    add_shortcode('show_count', 'custom_display_category_post_count');
Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Shortcode to Display Post Count in a Category’ is closed to new replies.