• 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 4 replies - 1 through 4 (of 4 total)
  • Samuel O.

    (@wpcoworker)

    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.

    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://wordpress.org/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

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.