Is it possible to do a if statement in a do_shortcode?
<?php
echo do_shortcode("[table width='500'] " .
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
the_content();
endwhile;
endif; .
"[/table]");
?>
It gives me an unexpected T_IF.
EDIT: And without the IF statement it gives the post outside the shortcode.
No.
echo do_shortcode()
is a function call. You can not pass a conditional statement as an argument of a function. What you need to do is pass in the returned result of your query as an argument of the function;Note:
In the example above, to prevent your content being echoed immediately you must call
get_the_content()
whichreturns
its value as opposed tothe_content()
which immediately echo’s the content. That is why your content appears outside of the shortcode when run without the incorrect conditional statement passed as an argument.Extended answer:
In light of your additional comment and extended question, to check for the existence of some “thing” or that some “thing” meets a particular condition you would conduct your testing outside of the function call to your shortcode much in the same way as we have done above;