0

I only touch php on the very basic stuff.

But I always like to avoid errors/notices when making wordpress themes.

I have a simple function below that I made to list my taxonomy terms.

$tax = 'dealer-communications';
$terms = get_terms($tax);
$count = count($terms);
if ( $count > 0 ){
    echo '<li class="nav-header">Dealer communications</li>';
    foreach ( $terms as $term ) {
        if (get_queried_object()->slug == $term->slug) $active = 'class="active"';
        echo '<li '.$active.'><a href="'.get_term_link($term->slug, $tax).'">' . $term->name . '</a></li>';
    }
}


As you can see I have a $active variable.

This active variable is undefined when the my get_queried_object()->slug does not match $term->slug

How can I avoid my active variable from being undefined. So it is defined but empty.

They only way my brain could work it out is by doing this...

$active = null;
if (get_queried_object()->slug == $term->slug) $active = 'class="active"';

or...

if (get_queried_object()->slug == $term->slug) {
    $active = 'class="active"';
} else {
    $active = '';
}


Is this the most efficient way of doing this or is there an alternative php method?


Many Thanks Josh

5 Answers 5

2

There is no alternative php method, but for readability you should not declare/init your variables in a if block, e.g. :

foreach ( $terms as $term ) {
    $active = '';
    if (get_queried_object()->slug == $term->slug) $active = 'class="active"';
    echo '<li '.$active.'><a href="'.get_term_link($term->slug, $tax).'">' . $term->name . '</a></li>';
}

You can also use ternary operator (but not really readable) :

$active = (get_queried_object()->slug == $term->slug) ? 'class="active"' : '';
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for comment. With ternary operator, how do you mean not readable?
I think ternary is readable in most case, but a lot of people don't think the same
1

The 2nd one will be the more efficient way :

if (get_queried_object()->slug == $term->slug) {
    $active = 'class="active"';
} else {
    $active = '';
}

1 Comment

Cool thanks for your comment, though am going to shorten with the ternary operator option.
1

The second approach is more common to see in my experience. You could of course shorten this using a ternary operator, similar to:

$active = (get_queried_object()->slug == $term->slug) ? 'class="active"' : '';
//       if ^                                            ^ do this        ^ else do this    

Some consider this to be more confusing though. I guess that boils down to personal preference.

1 Comment

Yeah I see why, but thanks for or the notes on the code, I can see whats happing now. Great thanks
1

You could also use the ternary operator as it's a bit shorter:

$active = ( get_queried_object()->slug == $term->slug ? 'class="active"' : '' );

Comments

-1

The second option is the best approach as it makes sure the variable value actually changes on every loop, otherwise there is a possibility that the value of the previous loop will affect the current one.

For example, you get to an active LI, set $active to the correct value and all works ok. However, on next loop step the LI should not be active, but because you are not clearing the previous assignment the $active variable will still set this LI to active.

EDIT: PHP scope does not work like javascript scope, some coments on this answer seem to require this clarification:

$test = array(
  array("name"=>"Is inactive", "active"=>false),
  array("name"=>"Is active", "active"=>true),
  array("name"=>"Is caught by problem", "active"=>false)
);

foreach ($test as $example ){
  if ($example["active"]) $active = true;

  if ($active) {
    echo $example["name"]." was parsed as ACTIVE\n";
  } else {
    echo $example["name"]." was parsed as INACTIVE\n";
  }
}

Outputs a notice (because $active is undefined on the first step of the loop) and the following text:

Is inactive was parsed as INACTIVE
Is active was parsed as ACTIVE
Is caught by problem was parsed as ACTIVE <--- Problem

3 Comments

You are wrong since you can set $active default value in the loop
If you use any of the sugested approaches, the 1st piece of code in the question doesn't.
I guess the it cant come out the loop because of scope, so its a new variable every time the loop starts.

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.