Tag Groups
[resolved] How to display tag groups automatically (4 posts)

  1. masterwoo
    Posted 3 years ago #

    How to display tag groups automatically when clicking a specific category. So for example I want only X group to be display when clicking on category1 and only Y group to be displayed when clicking category2.


  2. Chris
    Plugin Author

    Posted 3 years ago #

    You could modify your theme, or you automatically modify the query. For example you could add the following into the function.php of your active theme:

    add_filter( 'pre_get_posts', 'change_tag_group_for_category' );
    function change_tag_group_for_category( $query ) {
    	if ( $query->is_category() && $query->is_main_query() ) {
    		// set here which term groups to display; pattern: category id => term group id
    		$term_group = array(
    			1 => 2,
    			5 => 4
    		$posttags = get_tags();
    		// make array of tags for each listed category according to their groups
    		foreach($posttags as $tag)
    			foreach($term_group as $key=>$value)
    				if ($value == $tag->term_group) $query_tags[$key][] = $tag->term_id;
    		$category_id = get_cat_ID( $query->query_vars['category_name'] );
    		$query->set( 'tag__in', $query_tags[$category_id] );

    (It works only if all listed tag groups contain at least one tag.)

  3. Chris
    Plugin Author

    Posted 3 years ago #

    PS: I hope I correctly understood the question. This solution above will display posts that have tags in particular tag groups. It will not display tag groups. If you want to display tags instead, you simply display them with the function tag_groups_cloud.

  4. Chris
    Plugin Author

    Posted 3 years ago #

    Just to conclude this topic: If you need a different tag cloud on top of the posts on category pages, you add into your theme's category.php something like:

    	if ( function_exists( 'tag_groups_cloud' ) ) {
    		$term_group = array(
    			37 => 3,
    			3 => 4,
    		echo tag_groups_cloud( array( 'show_tabs' => 0 , 'include' => $term_group[ get_cat_ID( single_cat_title('',false) ) ]) );

    I hope that one of these two answers matches the question! ;-)

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Tag Groups
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic