how do i make this loop work
-
01-10-2020 - |
Question
Apologies I am a PHp beginner and I have no idea why this loop is not working on my tag.php page any help would be welcome.
<?php get_header(); ?>
<div class="posts"><!-- BLOG -->
<!-- Shapes on sides -->
<div class="shapes_left"> </div>
<div class="shapes_right"> </div>
<?php if (is_tag()) { ?>
<div id="archive_title">
<h1><?php single_tag_title(); ?></h1>
<?php }?>
</div>
<div id="featured_home">
<?php $counter = 0;
while ( have_posts() ) {
$counter += 1;
if ( $counter > 5 ) {
break;
}
the_post();
?>
<article class="sticky">
<div class="desc">
<div class="desc_over"><h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2></div>
<?php the_post_thumbnail(large); ?>
</div>
</article>
<?php }?>
</div>
<?php while( have_posts() ) {
the_post();
// it's a post! Display the post!
} ?>
<div class="post_main">
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<ul class="postinfo">
<li>Posted by <?php the_author_link(); ?></li>
<li><?php the_time( 'jS F Y'); ?></li>
</ul>
<?php the_content( ''); ?>
<div class="read_more_blog"><a href="<?php the_permalink(); ?>">Read More</a></div>
</div>
<?php endwhile; ?>
<nav id="pagination"> <!-- PAGINATION FOR BLOG -->
<ul>
<li class="older"><?php next_posts_link( 'Older posts'); ?></li>
<li class="newer"><?php previous_posts_link( 'Newer posts'); ?></li>
</ul>
</nav>
</div>
<!-- END OF BLOG PAGINATION -->
<?php else : ?>
<div id="post">
<!-- 404 Messege -->
<h3>404 ERROR!!</h3>
<p>Sorry we can't seem able to find what you are looking for</p>
<p><a href="<?php echo home_url(); ?>">Click here to get back to the homepage</a>
</p>
</div>
<?php endif; ?>
<?php get_sidebar(); ?>
</div>
<?php get_footer(); ?>
Solution
You should clean it up some more but this will fix your code:
<?php get_header(); ?>
<div class="posts"><!-- BLOG -->
<!-- Shapes on sides -->
<div class="shapes_left"> </div>
<div class="shapes_right"> </div>
<?php if (is_tag()) { ?>
<div id="archive_title">
<h1><?php single_tag_title(); ?></h1>
<?php }?>
</div>
<div id="featured_home">
<?php $counter = 0;
while ( have_posts() ) {
$counter += 1;
if ( $counter > 5 ) {
break;
}
the_post();
?>
<article class="sticky">
<div class="desc">
<div class="desc_over"><h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2></div>
<?php the_post_thumbnail(large); ?>
</div>
</article>
<?php }?>
</div>
<?php
// I've added the missing if statement and corrected some php grammar
if ( have_posts() ) : while( have_posts() ) :
the_post();
// it's a post! Display the post!
?>
<div class="post_main">
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<ul class="postinfo">
<li>Posted by <?php the_author_link(); ?></li>
<li><?php the_time( 'jS F Y'); ?></li>
</ul>
<?php the_content( ''); ?>
<div class="read_more_blog"><a href="<?php the_permalink(); ?>">Read More</a></div>
</div>
<?php endwhile; ?>
<nav id="pagination"> <!-- PAGINATION FOR BLOG -->
<ul>
<li class="older"><?php next_posts_link( 'Older posts'); ?></li>
<li class="newer"><?php previous_posts_link( 'Newer posts'); ?></li>
</ul>
</nav>
</div>
<!-- END OF BLOG PAGINATION -->
<!-- because the if statement has been started the else will now work -->
<?php else : ?>
<div id="post">
<!-- 404 Messege -->
<h3>404 ERROR!!</h3>
<p>Sorry we can't seem able to find what you are looking for</p>
<p><a href="<?php echo home_url(); ?>">Click here to get back to the homepage</a>
</p>
</div>
<!-- now the if statement is ended -->
<?php endif; ?>
<?php get_sidebar(); ?>
</div>
<?php get_footer(); ?>
Hope this helps
OTHER TIPS
The correct iteration of the loop is:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
and ends here:
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
For more details on the loop see https://codex.wordpress.org/The_Loop
Also I am not sure if your $counter variable means anything in PHP...
$counter += 1;
If you are trying to add 1 to the $counter variable you should do:
$counter++;
This will increase the $counter variable by 1
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange