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