In WordPress theme development we can use single.php
to show the specific single post.
For this purpose the common practice is:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php the_content();?>
<?php endwhile; ?>
<?php endif; ?>
Why do I need looping to show a single post? Can any one give some valid reason?
this was something that killed me for years and I found the answer: No, you don't need to use the LOOP in single pages, you just call the_post() and you have all data needed
.... BUT ....
If you don't use the loop (while(have_posts())....) a hook "loop_end" is not called and if a plugin/process has any action on this hook, it won't work. So for safety reasons you should use the loop.
Also, people ask do I need to check for existence before the loop: if(have_posts())?
<?
if( have_posts() ):
while( have_posts() ):
the_post();
.....
endwhile;
endif
?>
No, you don't need to check
.... BUT ....
Checking allows you to include headers/titles before the loop and not having them if the loop is empty.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With