1

I'm passing some variables to a (wordpress) function as following:

wp_list_categories('exclude=4&title_li=');

Instead of hard coded values for exclude (ie. 4,7), I want to pass a variable in it.

I am trying following but getting syntax error.

$exclude = 4;
wp_list_categories('exclude='.<?php echo $exclude; ?> .'&title_li=');

Can you help fixing it? Thanks.

0

3 Answers 3

4

The easiest change would be this

wp_list_categories('exclude=' . $exclude . '&title_li=');

Alternatively, you can use double quotes then encase the variables in { } - in general, I prefer not to do this as it makes it slightly less obvious what you're doing, at a glance.

wp_list_categories("exclude={$exclude}&title_li=");
Sign up to request clarification or add additional context in comments.

Comments

3

You are already inside PHP code, so do not wrap the $exclude in <?php ?>

wp_list_categories('exclude='. $exclude .'&title_li=');

Even better, you can surround the whole thing in double quotes to interpolate $exclude within the rest of the string.

wp_list_categories("exclude=$exclude&title_li=");

Comments

1

The above answers are of course correct, but I can't help but wonder why you would want to pass a string in that way. I am referring to the fact that you are hard-coding the 'exclude=' and '$title_li=' components into the function argument, when they should probably be given their own variable names if you plan to make different kinds of category requests. What I mean:

$category = '4'
$head = 'exclude';
$foot = '$title_li='
wp_list_categories($head.$category.$foot);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.