WordPress.org

Ready to get started?Download WordPress

Forums

Tag Groups
[resolved] Use tag groups for php returns (13 posts)

  1. Markus Stefanko
    Member
    Posted 2 years ago #

    I want to use tag groups to display posts based on certain tag groups on my frontpage bundled.

    The top tag group would be for example with "featured" tag.
    Then below that I would have some other logical tag groups split.

    Is that possible with this plugin? So like in the regular wordpress functions you can specify if you want to print something, or just get the programmatical output in an array, I would very much like it to happen here too. Example, single_tag_title() prints the tag, whereas single_tag_title( '', false ) outputs the tag to a variable.

    That would be some great feature

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

  2. Chris
    Member
    Plugin Author

    Posted 2 years ago #

    Good idea to return the tags instead of outputting them. I'll consider that for a future version.

    If you just want to output the tags of one group without any tabs, [tag_groups_cloud show_tabs=0 include=1] should be sufficient.

  3. Chris
    Member
    Plugin Author

    Posted 2 years ago #

    Please check out the version 0.6.

  4. Markus Stefanko
    Member
    Posted 2 years ago #

    Great! Will do that, thanks

  5. Chris
    Member
    Plugin Author

    Posted 2 years ago #

    Just one more note: If you feel adventurous you can try the development version http://downloads.wordpress.org/plugin/tag-groups.zip where I added a new function (actually quite a simple one) that lets you check for a given post id if this post has at least one tag that is within a given tag group.

    post_in_tag_group($post_id, $tag_group_id)

    The function returns true or false.

    Don't know if that makes the task easier or more difficult. At least you can filter the posts before output.

  6. Markus Stefanko
    Member
    Posted 2 years ago #

    Great stuff! Thanks.

    One thing though, in the documentation please change
    <?php if ( function_exists( tag_groups_cloud ) ) print_r( tag_groups_cloud( array( 'order' => 'count' ), true ) ); ?> to
    <?php if ( function_exists( tag_groups_cloud ) ) print_r( tag_groups_cloud( array( 'orderby' => 'count', 'order' => 'DESC' ), true ) ); ?>

    Cheers!

  7. Chris
    Member
    Plugin Author

    Posted 2 years ago #

    oh, thanks!

  8. Markus Stefanko
    Member
    Posted 2 years ago #

    Aha! So the latest update adds a check in the other direction. Right now I don't need it, but I guess it might be interesting to display the current tag_group for a post.

    I will share though what I added to my template :

    // Load the tag groups
    if ( function_exists("tag_groups_cloud") ) $tag_groups = tag_groups_cloud( array( 'orderby' => 'count', 'order' => 'DESC' ), true );
    
    // Go through each tag group
    while ( $current_tag_group = array_shift($tag_groups) ){
      echo "<h1>".$current_tag_group["name"]."</h1>";
      // Show the top tags from the tag group with count
      // TODO : Add limit
      foreach($current_tag_group["tags"] as $value){
        echo "<a href='".$value["link"]."'>".$value["name"]."</a><sup>".$value["count"]."</sup> ";
      }
      // Get the tags for the tag group into array
      $current_tags = array();
      foreach($current_tag_group["tags"] as $value){
        $current_tags[] = $value["slug"];
      }
      // Get 5 articles based on the tags we extracted for the tag group
      $args = array('posts_per_page' => 5, 'tag' => implode(",",$current_tags));
      $three_recent = new WP_Query($args);
      if ( $three_recent->have_posts() ) :
        while ( $three_recent->have_posts() ) : $three_recent->the_post();
        // the_post loop, show content here
        endwhile;
      endif;
    }
  9. Markus Stefanko
    Member
    Posted 2 years ago #

    The reason I'm doing the loop twice currently is because I want to add a limit to the first loop ( only show the top 5 tags for a tag group ), whereas in the second loop I want to have all the possible tags in there for the query.

    Easily fixed with one loop though.

  10. Markus Stefanko
    Member
    Posted 2 years ago #

    // Load the tag groups
    if ( function_exists("tag_groups_cloud") ) $tag_groups = tag_groups_cloud( array( 'orderby' => 'count', 'order' => 'DESC' ), true );
    
    // Go through each tag group
    while ( $current_tag_group = array_shift($tag_groups) ){
      echo "<h1>".$current_tag_group["name"]."</h1>";
    
      $tags_shown_per_tag_group = 5; // Limiting to top 5 tags displayed
      $current_tags = array(); // Get the tags for the tag group into array
    
      foreach($current_tag_group["tags"] as $value){
        while($tags_shown_per_tag_group>0){
            // Show the top tags from the tag group with count
            echo "<a href='".$value["link"]."'>".$value["name"]."</a><sup>".$value["count"]."</sup> ";
        }
        $current_tags[] = $value["slug"];
        $tags_shown_per_tag_group--;
      }
    
      // Get 5 articles based on the tags we extracted for the tag group
      $args = array('posts_per_page' => 5, 'tag' => implode(",",$current_tags));
      $three_recent = new WP_Query($args);
      if ( $three_recent->have_posts() ) :
        while ( $three_recent->have_posts() ) : $three_recent->the_post();
        // the_post loop, show content here
        endwhile;
      endif;
    }
  11. Chris
    Member
    Plugin Author

    Posted 2 years ago #

    Thank you for sharing.

  12. IOTI
    Member
    Posted 1 year ago #

    Chris,

    I noticed a bug in your post_in_tag_group() function. If you supply an ID of a post that doesn't exist, or doesn't have any tags you'll get a Invalid argument supplied for foreach() warning.

    pretty quick fix.

    function post_in_tag_group($post_id, $tag_group_id) {
    /*
    checks if the post with $post_id has a tag that is in the tag group with $tag_group_id
    */
    
    	$tags = get_the_tags($post_id);
    
    	//check if tags exist
    	if($tags){
    
        	foreach( $tags as $tag ) {
    
        		if ($tag->term_group == $tag_group_id) return true;
        	}
    	}else {
    
    	    return false;
    	}
    }
  13. Chris
    Member
    Plugin Author

    Posted 1 year ago #

    Many thanks for reporting, I'll use your fix.
    The function also needs to return false if $tags is not empty, but the foreach loop is finished without results.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic