2014-01-06 48 views
0

我需要把一個變量放入查詢參數。如何將變量插入查詢參數?

我有這種情況:

<?php 

$variable = get_field(loop_category); 

$catquery = new WP_Query('category_name=***VARIABLE-HERE***'); 
while($catquery->have_posts()) : $catquery->the_post(); 
?> 

我怎麼能這樣做呢?

對不起,我的英語不好,我不是英文spoker!

謝謝!

回答

0
$catquery = new WP_Query('category_name="' . $variable . '"'); 
0

試試這個 -

$catquery = new WP_Query("category_name={$variable}"); 
0

您可以將數組傳遞給WP_Query

<?php 

$variable = get_field(loop_category); 
$args = array(
    'category_name' => $variable 
); 
$catquery = new WP_Query($args); 
while($catquery->have_posts()) : $catquery->the_post(); 
?>