Number of posts in category %count(varname)%

#47491
  • Resolved Carlo
    Rank Math free

    Hi there

    I can’t get the %count(varname)% snippet to correctly show the number of posts inside of a category.

    Example: %count(varname)% Of The Best %term% Blogs %sep% %sitename%

    Desired result: 10 Of The Best SEO Blogs | Rank Math
    Actual result: Of The Best SEO Blogs | Rank Math

    Would I have to create some sort of variable to achieve this?

    Any help much appreciated 👍

Viewing 5 replies - 1 through 5 (of 5 total)
  • Alberto
    Rank Math business

    Hello,

    Thank you for contacting Rank Math today.

    You can create a custom var that gets the number of posts in the current category by using the following filter:

    /**
     * Filter to add custom variables
     */
    add_filter( 'rank_math/vars/replacements', function( $vars ) {
     return $vars;
    });

    Looking forward to helping you. Thank you.

    Carlo
    Rank Math free

    Hey Alberto

    Thanks for your quick reply.

    I actually found a post looking for similar help, right after I posted this.
    https://support.rankmath.com/ticket/code-for-categories/

    Although I’m still having no joy with the suggested variable on that link.

    It looks as if it calculating all posts instead of each category and stripping out the rest of the title.

    Todd
    Rank Math free

    Hi Carlo,

    You have to pay attention to this line:
    'post_type' => 'post' //assign the post type name

    Change it to the post type name.

    Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.

    Carlo
    Rank Math free

    Thanks for replying Todd.

    Yeah the post type is ‘post’.

    If I add in the category number e.g

    
    'post_type' => 'post', 
    'cat'       => '36'
    

    this then correctly pulls in the total number of posts in the category into the title, but then I want this for more than 1 category. Also, it only shows the number in the title, not the title that’s been set up in the category?

    Hello,

    Thank you for contacting Rank Math today.

    The code on the other thread will create a custom title on category pages.

    I can suggest that you customize this line return $number." "."Egg Recipes"; with the actual title that you want to display on the pages.

    If you want to use this on posts then you will also need to replace this conditional with another suitable one:
    if(is_category()){

    Looking forward to helping you. Thank you.

    ​​​​​​

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

Viewing 5 replies - 1 through 5 (of 5 total)

The ticket ‘Number of posts in category %count(varname)%’ is closed to new replies.