Title: using if statement to load different &#8220;the loop&#8221;
Last modified: August 19, 2016

---

# using if statement to load different “the loop”

 *  Resolved [franclin](https://wordpress.org/support/users/franclin/)
 * (@franclin)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/using-if-statement-to-load-different-the-loop/)
 * I have three pages that display three different categories.
    1. our products 
   2. where to buy 3. press
 * When each page loads I want to show the category that relates to the page. In
   each case the way I layout the image and title is different. I thought I would
   use an if statement to see what page was loaded and then load the appropriate
   loop using an include.
 * This works great from the home blog page (aka products page) but not from the
   press page?? The include loads but after the second if statement it fails. Why?
 * **The index page**:
 *     ```
       <div id="mainContent">
       	<!-- *note: home a.k.a. the blog page has been changed to the product page using the word press administrator but it is still refered to as home in the scripting-->
   
       	<?php
       // generate info appropriate to the page being displayed
       if (is_home()) {
       	 include( TEMPLATEPATH . '/theLoop_poducts.php' );
       	include( TEMPLATEPATH . '/theLoop_where_to_buy.php' );
       	include( TEMPLATEPATH . '/theLoop_press.php' );
        <!-- this works like a charm -->
   
       } elseif (is_page()) {
               // we're looking at a static page.  Which one?
               if (is_page('in-the-press')) {
                    // our press page.
        		echo "<p>This is in the press!</p>";
       		include( TEMPLATEPATH . '/theLoop_press.php' );
   
               } else {
                     // catch-all for other pages
       			  include( TEMPLATEPATH . '/theLoop.php' );
               }
       } else {
               // catch-all for everything else (archives, searches, 404s, etc)
               echo "<p>Pedro offers you his protection.</p>";
       		 include( TEMPLATEPATH . '/theLoop.php' );
       }
       ?>
   
       </div><!--end of mainContent-->
       ```
   
 * **The include page**:
 *     ```
       <!-- the loop
       if category is "product" then display
       else say sorry no post vailable
       -->
   
       Product page has loaded
       			 <!-- Start the Loop. -->
        <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
   
       		<!-- The following tests if the current post is in category Products. -->
       		<!-- If it is, the div box is given the CSS class "products". -->
   
       		<?php if ( in_category('products') ) { ?>
       				<div class="product">
       				<!-- Display the Title as a link to the Post's permalink. -->
       				<a href="<?php the_permalink(); ?>">
       				<div id="title"><?php the_title(); ?></div>
   
       				<!-- Display the Post's Content in a div box. -->
       				<div class="entry"><?php the_content(); ?></div>
       				</a>
   
       				</div> <!-- closes the first div box -->
   
       		<?php } ?>
   
       		 <!-- Stop The Loop (but note the "else:" - see next line). -->
       		 <?php endwhile; else: ?>
   
       		 <!-- The very first "if" tested to see if there were any Posts to -->
       		 <!-- display.  This "else" part tells what do if there weren't any. -->
       		 <p>Sorry, no products available.</p>
   
       <!-- REALLY stop The Loop. -->
       <?php endif; ?>
       ```
   

Viewing 5 replies - 1 through 5 (of 5 total)

 *  [stvwlf](https://wordpress.org/support/users/stvwlf/)
 * (@stvwlf)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/using-if-statement-to-load-different-the-loop/#post-1268335)
 * try using custom page templates
    [http://codex.wordpress.org/Pages#Creating_Your_Own_Page_Templates](http://codex.wordpress.org/Pages#Creating_Your_Own_Page_Templates)
 * [http://www.expand2web.com/blog/custom-page-template-wordpress/](http://www.expand2web.com/blog/custom-page-template-wordpress/)
 *  Thread Starter [franclin](https://wordpress.org/support/users/franclin/)
 * (@franclin)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/using-if-statement-to-load-different-the-loop/#post-1268376)
 * Thanks I’ve tried that but the problem remains the same:
 * The first if ( do you have post = yes) works.
 * The second fails, but only in the template. ( if post category is “press” …display
   title etc.)
 *     ```
       <?php if (in_category('press') ) {
       		  echo "<p>category is press</p>";
       				the_title(); 
   
       		 } else {
       		 echo "<p>no category found</p>";
       		 } ?>
       ```
   
 * Does the template launch from a different location than the root? Should the 
   category be ../press or something weird like that? — Stumped.
 *  [stvwlf](https://wordpress.org/support/users/stvwlf/)
 * (@stvwlf)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/using-if-statement-to-load-different-the-loop/#post-1268397)
 * The value in in_category’s parentheses is either a category’s ID, slug, or name.
   It has nothing to do with disk locations at all.
 * I am still unclear on what your problem is.
 * You might want to restructure your if’s – you don’t need nested If’s
 *     ```
       if (is_home()) {
        ....
       } elseif is_page('in-the-press') {
        ...
       } elseif is_page() {
        ...
       } else {
        ...
       }
       ```
   
 *  Thread Starter [franclin](https://wordpress.org/support/users/franclin/)
 * (@franclin)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/using-if-statement-to-load-different-the-loop/#post-1268402)
 * I’ve got it!
 * you have to query the post before the loop not inside it as I have done.
 * >  Place a call to query_posts() in one of your Template files before The Loop
   > begins. The wp_query object will generate a new SQL query using your parameters.
   > When you do this, WordPress ignores the other parameters it receives via the
   > URL (such as page number or category). If you want to preserve that information,
   > you can use the $query_string global variable in the call to query_posts().
   > 
   > […more on query_posts](http://codex.wordpress.org/Template_Tags/query_posts)
 * So, the principal remains the same. Index file checks to see what page we are
   on. If the page is static and the page name is “press” then include file “theLoop_press”
   which loads only posts that are tagged with the “PRESS” tag.
 * The fixed code for the new and included loop is:
 *     ```
       this is the press loop
       <?php
       query_posts('category_name=press');
   
       global $more;
       // set $more to 0 in order to only get the first part of the post
       $more = 0; 
   
       // the Loop
       while (have_posts()) : the_post();
       ?>
   
       				<div class="press">
       				<!-- Display the Title as a link to the Post's permalink. -->
       						<a href="<?php the_permalink(); ?>">permalink</a>
       						<div id="title"><?php the_title(); ?></div>
   
       				<!-- Display the Post's Content in a div box. -->
       						<div class="entry">
       						<!-- the content of the post -->
       						<?php the_content('view full the article»'); ?>
       						</div>
       				</div> <!-- closes the first div box -->
   
       <?php
       endwhile;
       ?>
       ```
   
 *  Thread Starter [franclin](https://wordpress.org/support/users/franclin/)
 * (@franclin)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/using-if-statement-to-load-different-the-loop/#post-1268447)
 * *Note for those who are interested in using an “if” statement Vs. Templates :
 * After educating myself I have found that my “if” statement serves the same purpose
   as WP’s templates. The idea being “If page loading is X; use this layout.” The
   advantage of using a template is that your client can change or apply the new
   layout on their own without having to call you to change the if statement.
 * EG:
    1. “Portfolio page” has the same layout as “My photos page”. 2. Pre-christmas
   layout Vs Boxing day sales Vs regular season layout. (this could be a change 
   in the whole layout or just the header. No more calls from the client asking 
   for trivial changes)

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘using if statement to load different “the loop”’ is closed to new replies.

## Tags

 * [loop](https://wordpress.org/support/topic-tag/loop/)
 * [query_posts](https://wordpress.org/support/topic-tag/query_posts/)

 * 5 replies
 * 2 participants
 * Last reply from: [franclin](https://wordpress.org/support/users/franclin/)
 * Last activity: [16 years, 6 months ago](https://wordpress.org/support/topic/using-if-statement-to-load-different-the-loop/#post-1268447)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
