WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. masterwoo
    Member
    Posted 1 year 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.

    http://wordpress.org/extend/plugins/tag-groups/

  2. Chris
    Member
    Plugin Author

    Posted 1 year 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
    Member
    Plugin Author

    Posted 1 year 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
    Member
    Plugin Author

    Posted 1 year 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:

    <?php
    	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

About this Topic