mari
mari

Reputation: 229

php- passing variables to a function

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.

Upvotes: 1

Views: 183

Answers (3)

inlinestyle
inlinestyle

Reputation: 127

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

Upvotes: 1

Michael Berkowski
Michael Berkowski

Reputation: 270617

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

Upvotes: 3

Joe
Joe

Reputation: 15802

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

Upvotes: 4

Related Questions