Anders2 Posted August 5, 2022 Share Posted August 5, 2022 I use this code in functions.php // recent posts shortcode/ // @ https://digwp.com/2018/08/shortcode-display-recent-posts/ function shapeSpace_recent_posts_shortcode($atts, $content = null) { global $post; extract(shortcode_atts(array( 'cat' => '', 'num' => '5', 'order' => 'DESC', 'orderby' => 'post_date', 'offset' => '', ), $atts)); $args = array( 'cat' => $cat, 'posts_per_page' => $num, 'order' => $order, 'orderby' => $orderby, 'offset' => $offset, ); $output = ''; $posts = get_posts($args); foreach($posts as $post) { setup_postdata($post); $output .= the_tags() .''; } wp_reset_postdata(); return ''. $output .''; } add_shortcode('recent_posts', 'shapeSpace_recent_posts_shortcode'); When I then use this shortcode: [recent_posts num="1" offset="0" cat="39"] then I get the category name with a link to /tag/categoryname/ How do I modify the (bold line of) code so that I also get the total number of posts with this tag in this category, like this: uncategorized (5) Something like this? $output .= the_tags() .' <strong>('. count($a_variable) .')</strong>'; Thanks in advance Anders Quote Link to comment https://forums.phpfreaks.com/topic/315141-how-to-get-the-total-number-of-posts-in-a-category-with-a-certain-tag-in-wordpress/ Share on other sites More sharing options...
Anders2 Posted August 12, 2022 Author Share Posted August 12, 2022 Hi! The problem is solved. All I did was add: $posttags = get_the_tags(); if ($posttags) { foreach($posttags as $tag) { echo ' <strong>('. $tag->count .')</strong>'; } } directly after: $output .= the_tags() .''; I found the solution here (under "More Information"):https://developer.wordpress.org/reference/functions/get_the_tags/ I got to change from $tag->name to $tag->count Greetings Anders Quote Link to comment https://forums.phpfreaks.com/topic/315141-how-to-get-the-total-number-of-posts-in-a-category-with-a-certain-tag-in-wordpress/#findComment-1599276 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.