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.
The code below works perfectly in index.php file BUT unfortunately it doesn't work when copied it to a custom page template.
<?php
/**
* Template Name: custom page template
*/
get_header(); ?>
<?php if (have_posts()) : ?>
<?php $count = 0; ?>
<?php while (have_posts()) : the_post(); ?>
<?php $count++; ?>
<?php if ($count == 1) : ?>
<div class="item1">
<span>hello!</span<?php the_title(); ?>>
</div><!-- .item# -->
<?php elseif ($count == 2) : ?>
<div class="item2">
<?php the_title(); ?><span>Hi!</span
</div><!-- .item# -->
<?php elseif ($count == 3) : ?>
<div class="item3">
<!-- Put Your Stuff Here -->
</div><!-- .item# -->
<?php elseif ($count == 4) : ?>
<div class="item4">
<!-- Put Your Stuff Here -->
</div><!-- .item# -->
<?php elseif ($count == 5) : ?>
<div class="item5">
<!-- Put Your Stuff Here -->
</div><!-- .item# -->
<?php else : ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
Goal:
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.
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.
I have tried using the WP Query but still no luck. The code below returns "Sorry, no posts matched your criteria."
Partially Working WP Query Code
Here's my website where this code will appear but seems the not working
<?php
/**
* Template Name: Custom Page - Blog
*/
get_header(); ?>
<!-- START of WP Query -->
<?php $the_query = new WP_Query( array("post_type"=>'post')); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<?php $count++; ?>
<?php if ($count == 1) : ?>
<div class="item1">
<span>Post 1</span><?php the_title(); ?>
</div><!-- .item# -->
<?php elseif ($count == 2) : ?>
<div class="item2">
<span>Post 2</span><?php the_title(); ?>
</div><!-- .item# -->
<?php elseif ($count == 3) : ?>
<div class="item3">
<span>Post 3</span><?php the_title(); ?>
</div><!-- .item# -->
<?php elseif ($count == 4) : ?>
<div class="item4">
<span>Post 4</span><?php the_title(); ?>
</div><!-- .item# -->
<?php elseif ($count == 5) : ?>
<div class="item5">
<span>Post 5</span><?php the_title(); ?>
</div><!-- .item# -->
<?php elseif ($count >= 5 || $count <= 7) : ?>
<div class="item6">
<span>Post 6 to 7</span><?php the_title(); ?>
</div><!-- .item# -->
<?php elseif ($count >= 8 || $count <= 15) : ?>
<div class="item6">
<span>Post 8 onwards - </span><?php the_title(); ?><?php the_excerpt(); ?>
</div><!-- .item# -->
<?php elseif ($count >= 16) : ?>
<div class="item6">
<span>Post 8 onwards - </span><?php the_title(); ?><?php the_excerpt(); ?>
</div><!-- .item# -->
<?php
global $wp_query;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
) );
?>
<?php else : ?>
<?php endif; ?>
<?php endwhile; ?>
<?php else : ?>
<p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
<!-- END of WP Query -->
<?php get_footer(); ?>
</article>
<?php get_footer(); ?>
Appreciate your help on this. Thank you!
<?php if ( $the_query->have_posts() ) : ?>add<?php $count++; ?>.