0

I'm trying to show information on a template page I made in WordPress. I am trying to show posts with GPS data if they don't have a tag that says no-gps. I want to check which pages are missing the GPS data. Is there an easy way to do that? I'm thinking of using an if else statement to add a mark so that I can do a search for the pages in WordPress and manually add the missing GPS data. Any ideas are greatly appreciated though. How do I put the extra if else statement into the current if statement? Here's my code:

<?php if(!has_tag('no-gps')){
//the_tags();
echo '<span id="topgps"><strong>GPS</strong>: '; 
the_field("GPS");
echo '</span>';
} ?>

I want to write:

If there is a page that doesn't have the no-gps tag, with GPS data, show the value. This works now.

New bit: But if the GPS value is missing (no value at all) on a page that doesn't have the no-gps tag show a star ★ instead.

1 Answer 1

0

Try changing the code for the line with the_field("GPS"); to:

$gps = get_field("GPS");
echo ($gps == '') ? '*' : $gps;
2
  • That works nicely. Thank you very much ScottM. What does the question mark and colon mean? Feb 23, 2022 at 23:38
  • Update: I just found a page explaining it. The Ternary Operator in PHP: Trinary–operator 2008-10-29 Its syntax is unusual at first, but it allows you to simplify more complex conditionals into one line statements. The basic syntax of the ternary operator is: (condition) ? 'true' : 'false'; In other words, PHP will evaluate the condition. If the condition is true, the first value–after the question mark–is returned. If the condition is false, the second value–after the colon–is returned. Feb 23, 2022 at 23:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.