我试图创建一个php文件,我可以用它来显示wordpress的索引或页面的内容。这个问题不是wordpress特有的。
我有一个'while'需要在两个不同的地方关闭,这取决于是否显示索引或页面。我正试图用'if'关闭'while'
if (is_home()) { endwhile; }
// otherphp
if (is_page()) { endwhile; }
这不是工作虽然。完整代码如下:
<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>
我意识到这段代码有些低效和混乱,但目前我不关注效率。我只是想让它工作。
如何有条件地结束'while'?
在一个正常的while
中做一个正常的条件:
while (/* something */) :
// same for all
if (/* is home */) {
// do something only for home
}
endwhile;