Ireneo Crodua
Ireneo Crodua

Reputation: 398

another way to make "if statement" short

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 &amp; Amenities</a>

<?php } else { ?>

    <a class="none" href="#<?php the_field('features_&_amenities'); ?>">Features &amp; 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

Answers (3)

octans
octans

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 &amp; Amenities</a>";

Upvotes: 1

scrowler
scrowler

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 &amp; Amenities</a>

There are many other ways of doing this of course, this is just one.

Upvotes: 2

mikedugan
mikedugan

Reputation: 2393

<a class="
  <?php echo $features ? '' : 'none'; ?>" 
  href="#
  <?php the_field('features_&_amenities'); ?>">
  Features &amp; Amenities</a>

Upvotes: 2

Related Questions