How to get all the tags under a specified classified article - WordPress tutorial

Labels do not depend on categories, they are separate. Having said that, the only way to make all the tags of a specific category in use is to cycle through each post of the category and get the tag of each post

There is no local method in WordPress to do this. The reason is that labels do not depend on categories, they are separate. Having said that, the only way to make all the tags of a particular category in use is to cycle through each post of the category and get the tag of each post.

I have written a quick function to do this.

(including all tags in subcategories and posts)

Put this function in your functions.php file.

 function get_tags_in_use($category_ID, $type = 'name'){ // Set up the query for our posts $my_posts = new WP_Query(array( 'cat' => $category_ID, // Your category id 'posts_per_page' => -1 // All posts from that category )); // Initialize our tag arrays $tags_by_id = array(); $tags_by_name = array(); $tags_by_slug = array(); // If there are posts in this category, loop through them if ($my_posts->have_posts()): while ($my_posts->have_posts()): $my_posts->the_post(); // Get all tags of current post $post_tags = wp_get_post_tags($my_posts->post->ID); // Loop through each tag foreach ($post_tags as $tag): // Set up our tags by id, name, and/or slug $tag_id = $tag->term_id; $tag_name = $tag->name; $tag_slug = $tag->slug; // Push each tag into our main array if not already in it if (! in_array($tag_id, $tags_by_id)) array_push($tags_by_id, $tag_id); if (! in_array($tag_name, $tags_by_name)) array_push($tags_by_name, $tag_name); if (! in_array($tag_slug, $tags_by_slug)) array_push($tags_by_slug, $tag_slug); endforeach; endwhile; endif; // Return value specified if ($type == 'id') return $tags_by_id; if ($type == 'name') return $tags_by_name; if ($type == 'slug') return $tags_by_slug; }

Then, when you want to get the label of a specific category, please call this function as follows:

 // First paramater is the category and the second paramater is how to return the tag (by name, by id, or by slug) // Leave second paramater blank to default to name $tags = get_tags_in_use(59, 'name');

I hope this can help.

Edit:

This is the function you need to use in combination with other functions:

 function tag_cloud_by_category($category_ID){ // Get our tag array $tags = get_tags_in_use($category_ID, 'id'); // Start our output variable echo '<div class="tag-cloud">'; // Cycle through each tag and set it up foreach ($tags as $tag): // Get our count $term = get_term_by('id', $tag, 'post_tag'); $count = $term->count; // Get tag name $tag_info = get_tag($tag); $tag_name = $tag_info->name; // Get tag link $tag_link = get_tag_link($tag); // Set up our font size based on count $size = 8 + $count; echo '<span style="font-size:'.$ size.'px; ">'; echo '<a href="'.$ tag_link.'">'.$ tag_name.'</a>'; echo ' </span>'; endforeach; echo '</div>'; }

Therefore, you can simply use this function as follows:

 tag_cloud_by_category($cat_id);
course

How to limit comment interval with pure code - WordPress tutorial

2020-11-11 0:25:00

course

When the homepage is opened, it will be automatically downloaded- WordPress Tutorial

2020-12-19 7:43:00

⚠️
Some codes and tutorials on Npcink come from the Internet, and are only for netizens to learn and exchange. If you like this article, you can Attach original link Reprint at will.
No intention of infringing your rights, please send an email to 1355471563#qq.com Or click on the right Private message: Muze feedback, we will deal with it as soon as possible.
0 replies A Author M administrators
    There is no discussion yet. Tell me your opinion
Personal Center
Shopping Cart
Coupon
Sign in today
There are new private messages Private Message List
search