Site logo

Archived topic

Post category count number inside button

9 replies · Started by Nacho on October 30, 2020

Viewing posts 1–10 of 10

Hello,

I´m trying to add the count of number of posts in a category inside a button that says something like "check the 40 posts inside this category". I´ve found this code:

add_action( 'generate_after_archive_description', function() {
    if ( is_category() ) {
        $category = get_category();
        $count = $category->category_count;

        if ( $count ) {
            echo 'This category has ' . $count . ' posts.';
        }
    }
} );

And this other code:

function count_cat_post($category) {
if(is_string($category)) {
    $catID = get_cat_ID($category);
}
elseif(is_numeric($category)) {
    $catID = $category;
} else {
    return 0;
}
$cat = get_category($catID);
return $cat->count;
}
<?php
 
// echo  count_cat_post('1');
// echo  count_cat_post('General');
 
?>

But now I don´t know how to show in the button the count number of the category.

Do you mind helping with it?

Thanks a lot and have a good day.

Hi there,

where will this button be added ? Will it be in the single post?

Hello David,

Yes, the button will be in single posts (one for each category).

Thanks a lot.

Try this for your function:

function single_post_cat_link_with_count() {
    $category = get_the_category();
    $first_cat = $category[0];
    printf( 
        '<a class="button" href="%1$s" title="%2$s">%3$s <span class="cat-count">%4$s</span></a>',
        esc_url( get_category_link( $first_cat->cat_ID) ),
        esc_attr( $first_cat->cat_name ),
        esc_html( $first_cat->cat_name ),
        esc_html( $first_cat->category_count )
    );
}

Then you can call the function like so:

single_post_cat_link_with_count();

Hello David,

Sorry I´m not savvy on code.

So what do I need to do with the code? Insert the function in code snippets and the "single_post..." in the post?

I´ve inserted the function in code snippets and the other in the post but nothing happens.

Thanks a lot.

The first part of code you can add to your functions.php

Then create a new Hook Element:

https://docs.generatepress.com/article/hooks-element-overview/

Add the second line of code to the hook content:

<?php single_post_cat_link_with_count(); ?>

Select the Hook where you want it positioned. In the article above you can see the Hooks in the visual guide.

Check Execute PHP.
And then set the Display Rules Location to Posts > All Posts

Hello David,

I´m getting what you mean.

But I want to show in a particular page several times depending on the chosen category.

For example, in the URL of this topic you can see there are several buttons, each should count a chosen category so it says something like "See the 34 posts of category news", "see the 5 posts of category taxes", ...

So how can I do that button?

Thanks a lot.

OK, so instead you would need to create a Shortcode function with this snippet:

function category_post_count( $atts ) {

    $atts = shortcode_atts( array(
        'category' => null
    ), $atts );
    // get the category by slug.
    $category = get_category_by_slug( $atts['category'] );
	ob_start();
    printf( 
        '<a class="button" href="%1$s" title="%2$s">%3$s <span class="cat-count">%4$s</span></a>',
        esc_url( get_category_link( $category ) ),
        esc_attr( $category->cat_name ),
        esc_html( $category->cat_name ),
        esc_html( $category->category_count )
    );
	return ob_get_clean();
}
add_shortcode( 'category_button', 'category_post_count' );

Then you can add a shortcode to your content:

[category_button category="category-slug"]

Hello David,

I´ve been testing till I got it working.

That really worked so thank you so much man, you made the day.

Have a really nice day :)

Awesome - glad to be of help!

This archived topic is closed to new replies.