- This topic has 9 replies, 2 voices, and was last updated 3 years, 5 months ago by David.
-
AuthorPosts
-
October 30, 2020 at 1:27 am #1510568Nacho
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.
October 30, 2020 at 8:23 am #1511290DavidStaffCustomer SupportHi there,
where will this button be added ? Will it be in the single post?
October 30, 2020 at 8:32 am #1511306NachoHello David,
Yes, the button will be in single posts (one for each category).
Thanks a lot.
October 30, 2020 at 9:24 am #1511368DavidStaffCustomer SupportTry 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();
October 30, 2020 at 1:40 pm #1511628NachoHello 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.
October 30, 2020 at 4:52 pm #1511735DavidStaffCustomer SupportThe 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 PostsOctober 31, 2020 at 6:42 am #1512194NachoHello 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.
October 31, 2020 at 9:00 am #1512542DavidStaffCustomer SupportOK, 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"]
November 2, 2020 at 2:37 am #1514172NachoHello 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 🙂
November 2, 2020 at 7:15 am #1514428DavidStaffCustomer SupportAwesome – glad to be of help!
-
AuthorPosts
- You must be logged in to reply to this topic.