I am trying to create a single php file I can use to display the content for the index or a page in wordpress. This question is not wordpress specific though.
I have a ‘while’ which needs to be closed in two different places depending on whether the index or a page is being displayed. I am trying to close the ‘while’ with an ‘if’
if (is_home()) { endwhile; }
// otherphp
if (is_page()) { endwhile; }
This is not working though. The full code is below:
<section id="main">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php if (is_home()) : ?> <article <?php post_class() ?> id="post-<?php the_ID(); ?>"> <?php elseif (is_page()) : ?> <article class="post" id="post-<?php the_ID(); ?>"> <?php else : NULL; endif; ?>
<div class="entry">
<?php the_content(); ?>
</div>
</article>
<?php if (is_home()) { endwhile; } ?> <!-- ENDWHILE MUST BE HERE ON INDEX PAGE -->
<?php if (is_home()) : get_template_part('nav','default'); elseif (is_page()) : NULL; else : NULL; endif; ?>
<?php if (is_page()) { endwhile; } ?> <!-- ENDWHILE MUST BE HERE ON ALL OTHER PAGES -->
<?php endif; ?>
</section>
I realise this code is somewhat inefficient and messy but at the moment I am not concentrating on efficiency. I am simply trying to make it work.
How can I conditionally end the ‘while’?
Just do a normal condition inside a normal
while: