简体   繁体   English

WordPress的get_terms参数传递问题

[英]Wordpress get_terms argument passing issue

I am facing a weird issue with get_terms() function of wordpress. 我在使用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>

is not returning any terms but when I use the value 1220 directly instead of $x, it is returning values. 没有返回任何条件,但是当我直接使用值1220而不是$ x时,它正在返回值。 The following code is working properly. 以下代码正常运行。

<?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>

I need to use variable as I will be getting term id from elsewhere. 我需要使用变量,因为我将从其他地方获取术语ID。 Please tell me what is the problem here. 请告诉我这里是什么问题。

Single quotes ' will print the $ sign as it is rather of showing the value of the variable 单引号'将按原样打印$符号,而不是显示变量的值

Consider this 考虑一下

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

in your case just change 在你的情况下只是改变

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

with double quotes " 用双引号"

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

or just concate the variable with the string with single (or double quotes) 或仅将变量与带单引号(或双引号)的字符串连接

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

Replace 更换

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

with

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM