Reputation: 398
is there any way to shorten this if statement?
<?php
$features = get_field('features_&_amenities');
if($features){ ?>
<a href="#<?php the_field('features_&_amenities'); ?>">Features & Amenities</a>
<?php } else { ?>
<a class="none" href="#<?php the_field('features_&_amenities'); ?>">Features & Amenities</a>
<?php } ?>
I use this to display my advance custom field data from database and I'm wondering is there any way to make it short if there any...
thank's for all the suggestion. and explanation.
Upvotes: 2
Views: 92
Reputation: 31
since it is a php file, it's better to separate the php code from html code.
$the_field = the_field('features_&_amenities');
$features = get_field('features_&_amenities');
$html_class = $features ? 'class="none"' : '';
echo "<a $html_class href=\"#$this_field\">Features & Amenities</a>";
Upvotes: 1
Reputation: 24406
The only difference between the two is class="none"
. Simply add that conditionally:
<?php
$features = get_field('features_&_amenities');
$class = !$features ? ' class="none"' : '';
?>
<a <?=$class?>href="#<?php the_field('features_&_amenities'); ?>">Features & Amenities</a>
There are many other ways of doing this of course, this is just one.
Upvotes: 2
Reputation: 2393
<a class="
<?php echo $features ? '' : 'none'; ?>"
href="#
<?php the_field('features_&_amenities'); ?>">
Features & Amenities</a>
Upvotes: 2