How to display thumbnail and title of the posts by particular tag name in WordPress

If you want to display the thumbnail with the title of the posts by specific tag name, you can use the following code snippet.

$tagname = "place"
query_posts( 'tag='.$tagname ) ?>
 <?php if ( have_posts() ) : ?>
  <?php while ( have_posts() ) : the_post(); ?>
    <div class="">
	<div class="p-thumb"><a href="<? the_permalink()?>" 
		rel="bookmark" title="<?php the_title_attribute(); ?>">
		<?php  if ( has_post_thumbnail() ) : ?>	
		   <?php the_post_thumbnail('thumbnail'); ?>
		<?php else : ?>
		   <img width="150" height="150" 
		   src="http://img.travellercode.com/tc/2016/10/coming-soon-150x150.jpg">
		<?php endif; ?>
		</a></div>
		<div class="p-title">
		<h5>
		<a href="<?php the_permalink()?>" 
		rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?>
		</a>
		</h5>
	</div>
  </div>  		
 <?php endwhile; ?>
<?php wp_reset_query(); ?>
<?php endif; ?>

In the above we are using query_posts() method. The query_posts function will completely override the main query so this must not be used within the WordPress Loop. And also should not be use in main query.

Want to do more

You can use more than one parameters with query_posts() methods. Suppose you want to get post of the particular tag and category. The following code returns all posts that belong to tag ‘place’ and category id 4.

<?php query_posts( 'tag='.$tagname .'&cat=4' ) ?>

If you want to get post by more than one tag. You can use query_posts() as:

<?php query_posts( 'tag=apples+oranges'  ) ?>

Author: Hirendra Sisodiya

Programmer at PSPL. Can be found on Google Plus.