[英]Wordpress get_terms argument passing issue
我在使用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。 請告訴我這里是什么問題。
單引號'
將按原樣打印$
符號,而不是顯示變量的值
考慮一下
$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" );
或僅將變量與帶單引號(或雙引號)的字符串連接
$terms = get_terms("topics", 'hide_empty=0&parent=' . $x );
更換
$terms = get_terms("topics", 'hide_empty=0&parent=$x' );
同
$terms = get_terms("topics", 'hide_empty=0&parent='.$x );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.