How to loop through pages with particular template.php

I would like to know how it is possible to loop through pages which have a particular template in WordPress. I have a template sale.php which is assigned to some pages (not posts). I want to do following:

  <div class="test">
    <?php $recent = new WP_Query("template_name=sale"); while($recent->have_posts()) : $recent->the_post();?>
        <?php the_title(); ?>
        <?php the_content(); ?>
    <?php endwhile; ?>
  </div>

obviously template_name=sale is not working. Is there something I can give as argument for the query to get only contents of all pages with a particular template.

Read More

Thanks

Related posts

Leave a Reply

1 comment

  1. You can use this line of code to loop through pages with particular template

    <?php is_page_template( $template ); ?> i mean

    if ( is_page_template( 'sale.php' ) ) {
    while (have_posts()) : the_post();
    the_content();
    endwhile;
     }  
     ?>
    

    and i hope this code will work for you