0

I'm getting a syntax error with this statement

$catlist = if (function_exists('put_cat_icons')) {
put_cat_icons( wp_list_categories('echo=0&orderby=id&show_count=1&title_li=&child_of=' . $cat_id.'&echo=0'));
} else {
wp_list_categories('echo=0&orderby=id&show_count=1&title_li=&child_of=' . $cat_id);
}​​​​​​​

If I remove $catlist = I get no errors. What's the problem in the code? Can I set a variable like this?

5 Answers 5

1

if as value of an assigment is invalid. Don#t know, where you get this from. However, you can use the ternary operator here

$catlist = function_exists('put_cat_icons')
    ? put_cat_icons( wp_list_categories('echo=0&orderby=id&show_count=1&title_li=&child_of=' . $cat_id.'&echo=0'))
    : wp_list_categories('echo=0&orderby=id&show_count=1&title_li=&child_of=' . $cat_id);

Also, when you look at your code, you may realized, that the wp_list_categories()-call is the same in both cases

$catlist = wp_list_categories('echo=0&orderby=id&show_count=1&title_li=&child_of=' . $cat_id);
if (function_exists('put_cat_icons')) $catlist = put_cat_icons($catlist);
Sign up to request clarification or add additional context in comments.

Comments

0

I suggest rearranging your code a little

if (function_exists('put_cat_icons')) {
    $catlist = put_cat_icons( wp_list_categories('echo=0&orderby=id&show_count=1&title_li=&child_of=' . $cat_id.'&echo=0'));
} else {
    $catlist = wp_list_categories('echo=0&orderby=id&show_count=1&title_li=&child_of=' . $cat_id);
}​​​​​​​

Comments

0

You cannot assign to the result of a condition - they must be freestanding. Possibly you meant:

if (function_exists('put_cat_icons')) {
    $catlist = put_cat_icons( wp_list_categories('echo=0&orderby=id&show_count=1&title_li=&child_of=' . $cat_id.'&echo=0'));
} else {
    $catlist = wp_list_categories('echo=0&orderby=id&show_count=1&title_li=&child_of=' . $cat_id);
}​​​​​​​

You may have got confused between ordinary and ternary conditions, the latter of which do allow assignment.

$something = 1 < 2 ? 'foo' : 'bar';

Comments

0

From the looks of it, you are mixing an ordinary if statement with the ternary operator which should look something like this $someVar = (condition) ? "TRUE VALUE" : "FALSE VALUE";

Using the ternary operator in your case would look something like this:

$catlist = (function_exists('put_cat_icons')) ? 
    put_cat_icons( wp_list_categories('echo=0&orderby=id&show_count=1&title_li=&child_of=' . $cat_id.'&echo=0')) : 
    wp_list_categories('echo=0&orderby=id&show_count=1&title_li=&child_of=' . $cat_id);

Comments

0

I dont this that is the correct way to set a variable. Are your trying to do this

$catlist = (function_exists('put_cat_icons')) ? put_cat_icons( wp_list_categories('echo=0&orderby=id&show_count=1&title_li=&child_of=' . $cat_id.'&echo=0')):
wp_list_categories('echo=0&orderby=id&show_count=1&title_li=&child_of=' . $cat_id);

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.