• Resolved cboakes

    (@cboakes)


    Hi,

    I was wondering if anybody is able to help me out with the following query:

    I am trying to display a list of tag titles with their respective post titles underneath in the sidebar. I put this together:

    <?php $posthead = get_posts(array('orderby' => 'name', 'order' => 'ASC', 'category' => '3,5,6,7,8'));	foreach($posthead as $post) : setup_postdata($post); ?>
     	<h2><?php $posttags = get_the_tags($post->ID);
    		foreach($posttags as $tag) {
    			echo $tag->name . ' ';
    		}
    	?></h2>
    	<p><?php the_title(); ?></p>
     <?php endforeach; ?>

    The issue with this is that the post will display multiple versions of the same tag title.

    Any help on how I could write this would be greatly appreciated.

    Thanks!

Viewing 7 replies - 1 through 7 (of 7 total)
  • Shouldn’t you first do get_the_tags, then iterate through each tag, get the posts for that tag, then iterate through the posts.

    Thread Starter cboakes

    (@cboakes)

    Thanks for the response Michael.

    I think I see what you mean, so, for instance if I was going to display the posts ID under the tags title I would do as follows:

    <?php $posthead = get_posts(array('orderby' => 'name', 'order' => 'ASC', 'category' => '3,5,6,7,8'));	foreach($posthead as $post) : setup_postdata($post); ?>
     		<?php $posttags = get_the_tags($post->ID);
    		foreach($posttags as $tag) {
    			echo '<h2>' . $tag->name . '</h2>';
    			echo '<p>' . $post->ID . '</p>';
    		}
    	?>
     <?php endforeach; ?>

    However my main problem is that it will display the tag title and then the post title underneath for each post. So for example if I had two posts tagged with ‘music’, then it would look like this:

    Music
    band_1
    Music
    band_2

    Thanks

    Thread Starter cboakes

    (@cboakes)

    I figured it out, here’s how I did it for anybody interested:

    <?php
    	$tags = get_tags( array('orderby' => 'name', 'order' => 'ASC') );
    	foreach ( (array) $tags as $tag ) {
    		$args=array('orderby' => 'date', 'showposts' => -1, 'tag__in' => array($tag->term_id), 'caller_get_posts'=>1);
    		$posts=get_posts($args);
    		echo '<h2>' . $tag->name . '</h2> <br />';
    		foreach($posts as $post) {
    			setup_postdata($post); ?>
    			<p><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></p>
    <?php }	} ?>

    I found this topic on how to do the same thing with categories extremely useful 🙂

    Thanks for your help!

    You’d be better off looping passing an array of the tags into tag__in …

    Why?..

    You’re looping over the tags and performing 1 query for each tag, so that’s 100 queries for 100 tags, etc…

    If you arrayed the tags first (pretty sure they return in an array already anyway), then passed the array into get_posts, you’d get it all done with 1 single query.. rather then a query for each..

    Thread Starter cboakes

    (@cboakes)

    I’ve tried my best to do this but it isn’t working for me – I know it’s a lot to ask but is there a way you could give me an example please?

    So i’m clear, you’re viewing a single post, grabbing the tags for that post, and wanting to query for all posts that have any of these tags.

    Then display them like…

    – Tag 1
    — Post with this tag 1
    — Post with this tag 2
    — Post with this tag 3
    – Tag 2
    — Post with this tag 1
    — Post with this tag 2
    — Post with this tag 3

    and so on.. , is that correct?

    Thread Starter cboakes

    (@cboakes)

    Hi,

    Thanks for the reply but I’ve just figured it out. Here’s how I ended up doing it. I realise that this may not the most perfect method but it works. If you have any suggestions post them up.

    To clarify: the following code looks to see if a post has more than one tag, if it does it displays that tag as a header and displays the other tags with that tag below. If the post only has one tag it displays it as a link.

    To demonstrate.
    Post 1 has tags of ‘Music’ and ‘Rock’
    Post 2 has tags of ‘Music’ and ‘Jazz’
    Post 3 has a tag of ‘My Band’

    It would look like this:
    -Music
    –Rock
    –Jazz
    -My Band

    <?php
    	//Set up an array to put all of the tags that have been used in drop-dwon
    	$used = array();
    	//Get all the tags
    	$tags = get_tags(array('orderby' => '', 'order' => 'ASC') );
    	//For each tag
    	foreach ($tags as $tag) {
    		$post_tag = get_term($tag, 'post_tag');
    		//Get posts limited only to the current tag
    		$t_posts=get_posts(array('orderby' => 'date', 'tag__in' => array($tag->term_id)));
    		//If the post has more than one tag
    		if ($post_tag->count > 1) {
    			//Display the drop down title (e.g if two posts were tagged with 'Music' this would diaply that 'Music' title
    			echo '<h4 id="' . $tag->term_id .'">' . $tag->name . '</h4> <br />';
    			//Declaring the title as a variable for later use
    			$taghead = $tag->name;
    			//For each post with the current tag
    			foreach($t_posts as $t_post) {
    				setup_postdata($t_post);
    				//Get all of the posts under the current title
    				$the_tags = get_the_tags($t_post->ID); foreach($the_tags as $the_tag) {
    				//Don't diaply the current tags title
    				if ($the_tag->name == $taghead) continue;
    				//Display the tag under the title
    				echo '<p class="tag-' . $the_tag->term_id . '"><a href="' . get_tag_link( $the_tag->term_id ) . '">' . $the_tag->name . '</a></p>';
    				//Push the current tag into the used array
    				array_push($used, $the_tag->term_id);
    			} //End foreach tag
    		} //End foreach
    		//If the post only has one tag then link to it as an archive
    		} else {
    			//If the tag has already been displayed above don't show it here
    			if (in_array($tag->term_id, $used)) continue;
    			//Display the rest of the tags
    			echo '<h4 class="tag-' . $tag->term_id . '"><a href="'. get_tag_link( $tag->term_id ) . '">' . $tag->name . '</a></h4><br />';
    		} // End else
    ?>
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Display Post Titles Under Tag Name in Sidebar’ is closed to new replies.