【发布时间】:2019-02-17 22:46:13
【问题描述】:
我想采用基本的 WordPress 循环,并在它调用内容模板的地方,我想运行一个 if else 语句,该语句根据类别拉入不同的模板部分。我知道我可以制作一堆archive.php 的副本,但是为什么要为您想要使用的每个类别都这样做呢?
所以我尝试了这段代码,但它只是在每种情况下都拉入了存档模板:
while ( have_posts() ) :
the_post();
if ( is_category( 'my-category-slug' )) {
get_template_part( 'template-parts/content', 'single' );
} else {
get_template_part( 'template-parts/content', 'archive' );
}
endwhile;
那么我什至可以在循环中运行这样的 if else 语句吗?
【问题讨论】:
标签: php wordpress if-statement while-loop