2014-02-20 49 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);