在wordpress category.php中我有这段代码:
if ( is_category('cat-1') ) {
$the_query = new WP_Query( array ( 'post_type' => 'mueble', 'category_name' => 'cat-1' , 'posts_per_page' => 3 ) ) ;
}
if ( is_category('cat-2') ) {
$the_query = new WP_Query( array ( 'post_type' => 'mueble', 'category_name' => 'cat-2' , 'posts_per_page' => 3 ) ) ;
}
if ( is_category('cat-3') ) {
$the_query = new WP_Query( array ( 'post_type' => 'mueble', 'category_name' => 'cat-3' , 'posts_per_page' => 3 ) ) ;
}
while ( $the_query->have_posts() ) : $the_query->the_post();
...
endwhile; wp_reset_postdata();
如何简化代码?我有30个类别,太多的条件不理想...
答案 0 :(得分:0)
如果您知道确切的类别数,可以尝试for循环:
for ($x=0; $x<=30; $x++) {
if ( is_category('cat-' . $x) ) { $the_query = new WP_Query( array ( 'post_type' => 'mueble', 'category_name' => 'cat-' . $x , 'posts_per_page' => 3 ) ) ; }
}