WordPress.org

Ready to get started?Download WordPress

Forums

Get posts from another category with same tag as current post? (3 posts)

  1. 3stripe
    Member
    Posted 5 years ago #

    This is a bit complicated to explain so bear with me...

    Q: Inside the loop of a category template, how can I spit out a list of other posts from another category which share the same tag as the current post?

    Eg. On my 'Bands' overview page, I have a list of bands (each one of which is a post in the 'Bands' category), and beside each band I want to show the latest 3 releases (a post from the 'Releases category) and the first image from each post. Each band and release post is tagged with a band name.

    $artist is the tag slug but I don't think it's working inside the get_posts query....

    Thanks for reading :)

    Inside the loop of a category template:

    <?php
    $posttags = get_the_tags();
    if ($posttags) {
    	foreach($posttags as $tag) {
    		$artist = $tag->slug . ' ';
    	}
    }
    ?>
    
    <!-- Latest releases -->
    <div class="span-4 last">
    	<h3>~ Latest Releases</h3><?php /* just to test that we have the artist slug... */ echo $artist; ?>
    	<ul>
    	<?php
    		$rand_posts = get_posts('numberposts=1&tag=$artist&category=releases');
    		foreach( $rand_posts as $post ) :
    		?>
    		<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    		<?php endforeach; ?>
    		</ul>
    </div>
  2. 3stripe
    Member
    Posted 5 years ago #

    PS. I could use a related post plugin but it's not quite flexible enough ;)

  3. 3stripe
    Member
    Posted 4 years ago #

    I never did figure this out :(

Topic Closed

This topic has been closed to new replies.

About this Topic