2014-02-20 48 views
0

我正在面對wordpress的get_terms()函數的一個奇怪問題。Wordpress get_terms參數傳遞問題

<?php $x=1220; 
$terms = get_terms("topics", 'hide_empty=0&parent=$x'); 
<ul class="ul1"> 
<?php foreach ($terms as $term) { ?> 
<li><a href="<?php echo get_term_link($term->slug, 'topics'); ?>"> 
<?php echo $term->name; ?></a> 
</li><?php }; ?> </ul> 

沒有返回任何條款,但是當我用值1220,而不是直接的$ X,它返回的值。以下代碼正常工作。

<?php $terms = get_terms("topics", 'hide_empty=0&parent=1220'); 
<ul class="ul1"> 
<?php foreach ($terms as $term) { ?> 
<li><a href="<?php echo get_term_link($term->slug, 'topics'); ?>"> 
<?php echo $term->name; ?></a> 
</li><?php }; ?> </ul> 

我需要使用變量,因爲我將從別處獲取術語ID。請告訴我這裏有什麼問題。

回答

0

單引號'將打印$跡象,因爲這是相當展示的可變

價值的考慮這個

$a = 9; 
echo '$a'; // output = $a 
echo "$a"; // output = 9 

你的情況只是改變

$terms = get_terms("topics", 'hide_empty=0&parent=$x'); 

用雙引號"

$terms = get_terms("topics", "hide_empty=0&parent=$x"); 

或只是concate用單(或雙引號)將字符串變量

$terms = get_terms("topics", 'hide_empty=0&parent=' . $x); 
0

更換

$terms = get_terms("topics", 'hide_empty=0&parent=$x'); 

$terms = get_terms("topics", 'hide_empty=0&parent='.$x);