Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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;

//assuming you have taken care of your query prior to this point
if ( have_posts() ) : while ( have_posts() ) : the_post();

    $content = get_the_content(); //store content in variable

    echo do_shortcode("[table width='500'] ". $content . "[/table]"); 

endwhile; endif;

Note:

In the example above, to prevent your content being echoed immediately you must call get_the_content() which returns its value as opposed to the_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;

//assuming you have taken care of your query prior to this point
if ( have_posts() ) : while ( have_posts() ) : the_post();

    $content = get_the_content(); //store content in variable

    $thing = get_post_meta( get_the_ID(), 'thing', true );

    if ($thing === "yes") {

       //assuming you want to concatenate your content with an image
       $content = $content . '<br> <img src="path_to_image" />';

    } 

    echo do_shortcode("[table width='500'] ". $content . "[/table]"); 

endwhile; endif;
share|improve this answer
    
Thanks! I knew this, but just forgot it. –  Disgeae Jun 28 '13 at 7:19
    
How do I check if an argument is equal to something inside the do_shortcode? Like: 'if $thing == yes { echo 'blabla '}'? –  Disgeae Jun 28 '13 at 8:55
    
What is $thing supposed to equal and what are you checking that should be equal to $thing? –  userabuser Jun 28 '13 at 9:05
    
It's for a custom post type display, now it's displaying yes/no with: "esc_html( get_post_meta( get_the_ID(), 'thing', true ) )" inside the do_shortcode. But it want to give it a image with a check or cross instead of the text yes/no. –  Disgeae Jun 28 '13 at 9:07
    
...additional information provided above. –  userabuser Jun 28 '13 at 9:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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