This seems like a very simple question but everything I try, gives me an error.
if ( $query_results->have_posts() ) :
$count_results = $query_results->found_posts;
Question: How to add else
statement/functionality to this code?
I've tried to add parentheses & else
, I've also tried all combinations with :
and ?
. Nothing works..
You're not using the ternary operator correctly. Try:
That's not a ternary operator; it's an uncommon
if
block syntax. Try this:Note: there will be an
endif;
later in your code, which you also need to replace using the above syntax. There may also beelseif (...):
orelse :
statements. While these are acceptable and work, they are generally considered more difficult to read, and you are better off using braces, as in my example above.For what it's worth, the only project I have ever seen use the colon syntax (
if (...):
) is Wordpress. I'm sure they had a reason for doing so, but it's definitely the less common syntax.