Eclectic Solutions
Eclectic Solutions

Reputation: 53

Wordpress get_terms argument passing issue

I am facing a weird issue with get_terms() function of wordpress.

<?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. 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. Please tell me what is the problem here.

Upvotes: 0

Views: 431

Answers (2)

zzlalani
zzlalani

Reputation: 24384

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 );

Upvotes: 1

Jenz
Jenz

Reputation: 8369

Replace

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

with

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

Upvotes: 0

Related Questions