title: Custom Wordpress Loop not displaying when in Page Template
body: <p>I have a custom Wordpress loop in my index file which is not currently working. The purpose of this custom WP loop is to assign different classes and structure based on its post number.</p>
  
  <p>The code below works perfectly in index.php file <strong>BUT</strong> unfortunately it doesn't work when copied it to a custom page template.</p>
  
  <pre><code>&lt;?php
  /**
  * Template Name: custom page template
  */
  get_header(); ?&gt;
  
  &lt;?php if (have_posts()) : ?&gt;
  &lt;?php $count = 0; ?&gt;
  &lt;?php while (have_posts()) : the_post(); ?&gt;
  &lt;?php $count++; ?&gt;
  &lt;?php if ($count == 1) : ?&gt;
  
  &lt;div class="item1"&gt;
  &lt;span&gt;hello!&lt;/span&lt;?php the_title(); ?&gt;&gt;
  &lt;/div&gt;&lt;!-- .item# --&gt; 
  
  &lt;?php elseif ($count == 2) : ?&gt;      
  
  &lt;div class="item2"&gt;
  &lt;?php the_title(); ?&gt;&lt;span&gt;Hi!&lt;/span
  &lt;/div&gt;&lt;!-- .item# --&gt; 
  
  &lt;?php elseif ($count == 3) : ?&gt;      
  
  &lt;div class="item3"&gt;
  &lt;!-- Put Your Stuff Here --&gt;
  &lt;/div&gt;&lt;!-- .item# --&gt; 
  
  &lt;?php elseif ($count == 4) : ?&gt;      
  
  &lt;div class="item4"&gt;
  &lt;!-- Put Your Stuff Here --&gt;
  &lt;/div&gt;&lt;!-- .item# --&gt; 
  
  &lt;?php elseif ($count == 5) : ?&gt;      
  
  &lt;div class="item5"&gt;
  &lt;!-- Put Your Stuff Here --&gt;
  &lt;/div&gt;&lt;!-- .item# --&gt;
  
  &lt;?php else : ?&gt;
  
  &lt;?php endif; ?&gt;
  &lt;?php endwhile; ?&gt;
  &lt;?php endif; ?&gt;
  </code></pre>
  
  <p><strong>Goal:</strong></p>
  
  <p>What I'm trying to achieve is to create a custom page (let's say) www.mywebsite.com/my-custom-page that lists down all articles.</p>
  
  <p>As mentioned above, the custom loop is not displaying in the page as well as the numbered pagination. As if the page template doesn't recognized or ignores the custom loop codes.</p>
  
  <p>I have tried using the <a href="https://codex.wordpress.org/Class_Reference/WP_Query" rel="noreferrer">WP Query</a> but still no luck. The code below returns <em>"Sorry, no posts matched your criteria."</em></p>
  
  <p><strong>Partially Working WP Query Code</strong></p>
  
  <p>Here's my <a href="https://www.resumezilla.net/blog/" rel="noreferrer">website</a> where this code will appear but seems the not working </p>
  
  <pre><code>&lt;?php
  /**
  * Template Name: Custom Page - Blog
  */
  get_header(); ?&gt;
  
  
  &lt;!-- START of WP Query --&gt;
  
  &lt;?php $the_query = new WP_Query( array("post_type"=&gt;'post')); ?&gt;
  
  &lt;?php if ( $the_query-&gt;have_posts() ) : ?&gt;
  
  &lt;?php while ($the_query-&gt;have_posts()) : $the_query-&gt;the_post(); ?&gt;
  
  &lt;?php $count++; ?&gt;
  
      &lt;?php if ($count == 1) : ?&gt;
      &lt;div class="item1"&gt;
          &lt;span&gt;Post 1&lt;/span&gt;&lt;?php the_title(); ?&gt;
      &lt;/div&gt;&lt;!-- .item# --&gt; 
  
      &lt;?php elseif ($count == 2) : ?&gt;      
      &lt;div class="item2"&gt;
      &lt;span&gt;Post 2&lt;/span&gt;&lt;?php the_title(); ?&gt;
      &lt;/div&gt;&lt;!-- .item# --&gt; 
  
      &lt;?php elseif ($count == 3) : ?&gt;      
      &lt;div class="item3"&gt;
          &lt;span&gt;Post 3&lt;/span&gt;&lt;?php the_title(); ?&gt;
      &lt;/div&gt;&lt;!-- .item# --&gt; 
  
      &lt;?php elseif ($count == 4) : ?&gt;      
      &lt;div class="item4"&gt;
          &lt;span&gt;Post 4&lt;/span&gt;&lt;?php the_title(); ?&gt;
      &lt;/div&gt;&lt;!-- .item# --&gt; 
  
      &lt;?php elseif ($count == 5) : ?&gt;      
      &lt;div class="item5"&gt;
          &lt;span&gt;Post 5&lt;/span&gt;&lt;?php the_title(); ?&gt;
      &lt;/div&gt;&lt;!-- .item# --&gt;
  
      &lt;?php elseif ($count &gt;= 5 || $count &lt;= 7) : ?&gt;      
      &lt;div class="item6"&gt;
          &lt;span&gt;Post 6 to 7&lt;/span&gt;&lt;?php the_title(); ?&gt;
      &lt;/div&gt;&lt;!-- .item# --&gt;
  
      &lt;?php elseif ($count &gt;= 8 || $count &lt;= 15) : ?&gt;      
      &lt;div class="item6"&gt;
          &lt;span&gt;Post 8 onwards - &lt;/span&gt;&lt;?php the_title(); ?&gt;&lt;?php the_excerpt(); ?&gt;
      &lt;/div&gt;&lt;!-- .item# --&gt;
  
      &lt;?php elseif ($count &gt;= 16) : ?&gt;      
      &lt;div class="item6"&gt;
          &lt;span&gt;Post 8 onwards - &lt;/span&gt;&lt;?php the_title(); ?&gt;&lt;?php the_excerpt(); ?&gt;
      &lt;/div&gt;&lt;!-- .item# --&gt;
  </code></pre>
  
      
  
  <pre><code>    &lt;?php
      global $wp_query;
  
          $big = 999999999; // need an unlikely integer
  
          echo paginate_links( array(
          'base' =&gt; str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
          'format' =&gt; '?paged=%#%',
          'current' =&gt; max( 1, get_query_var('paged') ),
          'total' =&gt; $wp_query-&gt;max_num_pages
          ) );
      ?&gt;
  
  
  &lt;?php else : ?&gt;
  
  &lt;?php endif; ?&gt;
  &lt;?php endwhile; ?&gt;
  &lt;?php else : ?&gt;
  &lt;p&gt;&lt;?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?&gt;&lt;/p&gt;
  &lt;?php endif; ?&gt;
  
  &lt;!-- END of WP Query --&gt;
  
  
  &lt;?php get_footer(); ?&gt;
  
      &lt;/article&gt;
  
  &lt;?php get_footer(); ?&gt;
  </code></pre>
  
  <p>Appreciate your help on this. Thank you!</p>