2014-01-14 80 views
0

在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

如果你知道類別的確切數量,你可以嘗試一個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)) ; } 
}