I am attempting to display the correct HTML under a "p" tag with PHP based on a true/false value which is pulled from WordPress ACF plugin, for every individual post. It's my first time using PHP and I'm not sure how to go about this.
My code is:
<p >
<?php if (the_field('relocation', $job->ID) === 1) ?>
</p>
Currently, if the post has "relocation" marked as false - an empty applied-grid-filter "p" tag appears. If the post has "relocation" marked as true - an applied-grid-filter "p" tag with the number "1" for true appears.
How can I make it so that when it is marked false, no P tag is made at all - and when the post is marked true, a P tag with textcontent of "Relocation" is made?
edit full context:
<section >
<?php if (count($jobs)) { ?>
<div >
<?php foreach ($jobs as $job) { ?>
<div >
<h2 ><?= esc_html($job->post_title) ?></h2>
<div >
<h2 ><?php the_field('minimum_salary', $job->ID) ?>K
- <?php the_field('maximum_salary', $job->ID) ?>K</h2>
<div >
<img
src="<?= get_template_directory_uri() ?>/assets/location-icon.png"
/>
<p >Louisville, KY</p>
</div>
</div>
<p >
<?php the_field('short_description', $job->ID) ?>
</p>
<div >
<?php foreach (get_terms('industry', ['object_id' => $job->ID]) as $term) { ?>
<p ><?= esc_html($term->name) ?></p>
<?php } ?>
<?php foreach (get_terms('job_type', ['object_id' => $job->ID]) as $term) { ?>
<p ><?= esc_html($term->name) ?></p>
<?php } ?>
<?php
if (the_field('relocation', $job->ID) === 1) { ?>
<p >Relocation</p>
<?php } ?>
<?php
if (the_field('travel', $job->ID) === 0) { ?>
<p >No Travel</p>
<?php } ?>
</div>
<a href="<?php the_permalink($job->ID) ?>">View More</a>
</div>
<?php } ?>
</div>
<?php } else { ?>
<p >No results found! Try another search.</p>
<?php } ?></section>
CodePudding user response:
Just move the html code inside your php code. You also might wanna use get_field
instead of the_field
. Also consider using the equal ==
instead of the identical ===
comparison operator which will be tolerant if the return value is not a number but for example true/false
.
Also have a look at https://www.php.net/manual/en/language.operators.comparison.php for this.
<?php
if (get_field('relocation', $job->ID) == 1) { ?>
<p ></p>
<?php }; ?>