[Resolved] Post category count number inside button

Home Forums Support Post category count number inside button

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #1510568
    Nacho

    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.

    #1511290
    David
    Staff
    Customer Support

    Hi there,

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

    #1511306
    Nacho

    Hello David,

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

    Thanks a lot.

    #1511368
    David
    Staff
    Customer Support

    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();

    #1511628
    Nacho

    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.

    #1511735
    David
    Staff
    Customer Support

    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

    #1512194
    Nacho

    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.

    #1512542
    David
    Staff
    Customer Support

    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"]

    #1514172
    Nacho

    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 🙂

    #1514428
    David
    Staff
    Customer Support

    Awesome – glad to be of help!

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