0

I've got a shortcode that's got HTML in an $output field that's lovely. I just want to add one paragraph to it at the bottom that's conditional on this statement:

<?php if ( get_post_meta( get_the_ID(), 'campus', true ) ) : ?> 

I can't quite figure out how to format a statement like that (if possible) into an existing $output field.

Here's what the $output looks like at present, I want to add this condition on line 8 and basically have another paragraph tag with some content there that shows there only if the statement is true:

'<div class="w-blog-entry" style="padding:0;">
                                <div class="w-blog-entry-h">
                                <div class="l-subsection color_dark" style="background-image: url('.$the_thumbnail.'); background-position: center center; padding-top:0; padding-bottom:0; background-attachment: inherit;">
                                <div class="l-subsection-h">
                                    <div class="l-subsection-hh g-html i-cf" style="font-family:Century Gothic; text-shadow: 0px 2px 2px rgba(0, 0, 0, 0.3);">
                                        <a class="w-blog-entry-link" href="'.get_permalink(get_the_ID()).'"><h2 class="w-blog-entry-title" style="line-height:1em; margin-left:0; padding-left:4px; font-size:20px;"><span class="w-blog-entry-title-h" style="color:#fff;">'.get_post_meta( get_the_ID(), 'destination', true ).'<br><span style="font-size:0.6em; color:#f2f2f2;">'.get_post_meta( get_the_ID(), 'depart', true ).' <span style="text-transform: lowercase; font-size:0.8em;">to</span> '.get_post_meta( get_the_ID(), 'return', true ).'</span></span></h2></a>
                                        <p style="line-height:1.2em; padding-left:4px; font-size:0.9em;">'.get_post_meta( get_the_ID(), 'projectdesc', true ).'</p>                                      
                                    </div>
                                </div>
                            </div>

                            </div>';
    }

    $output .=          '</div>
                    </div>
                </div>'

Is this possible, or do I have to make a whole other condition to the shortcode with two outputs?

Let me know if you need more context from the shortcode, I just didn't want to waste too much space if my original goal is possible.

2
  • Sounds like you need to brush up on PHP control structures and string operators. Check out this question for an example.
    – bosco
    Commented Sep 13, 2015 at 0:47
  • To say I need to brush up is an understatement. Totally plan to do that after I'm done this project, thanks for sharing the resources, @bosco.
    – Ste7en
    Commented Sep 13, 2015 at 1:28

2 Answers 2

1

If I understand you right you just want to use a '?' type of if statement:

$output = '<div class="w-blog-entry" style="padding:0;">
                            <div class="w-blog-entry-h">
                            <div class="l-subsection color_dark" style="background-image: url('.$the_thumbnail.'); background-position: center center; padding-top:0; padding-bottom:0; background-attachment: inherit;">
                            <div class="l-subsection-h">
                                <div class="l-subsection-hh g-html i-cf" style="font-family:Century Gothic; text-shadow: 0px 2px 2px rgba(0, 0, 0, 0.3);">
                                    <a class="w-blog-entry-link" href="'.get_permalink(get_the_ID()).'"><h2 class="w-blog-entry-title" style="line-height:1em; margin-left:0; padding-left:4px; font-size:20px;"><span class="w-blog-entry-title-h" style="color:#fff;">'.get_post_meta( get_the_ID(), 'destination', true ).'<br><span style="font-size:0.6em; color:#f2f2f2;">'.get_post_meta( get_the_ID(), 'depart', true ).' <span style="text-transform: lowercase; font-size:0.8em;">to</span> '.get_post_meta( get_the_ID(), 'return', true ).'</span></span></h2></a>
                                    <p style="line-height:1.2em; padding-left:4px; font-size:0.9em;">'.get_post_meta( get_the_ID(), 'projectdesc', true ).'</p>';
$output .= get_post_meta( get_the_ID(), 'campus', true ) ? '<p>True so do this paragraph</p>' : '<p>false, so do this instead</p>';
$output.='                                    </div>
                            </div>
                        </div>

                        </div>';

A standard if else would have worked OK too so I'm not sure where the confusion was.

Another way is to use a php output buffer which would work like this:

ob_start();
?>
<div class="w-blog-entry" style="padding:0;"> etc etc
<?php if(get_post_meta( get_the_ID(), 'campus', true )) {?>
<p>True paragraph</p>
<?php }else{ ?>
<p>false paragraph</p>
<?php } ?>

And then when you've finished with all your output you put it in your $output variable like this:

$output = ob_get_clean();

When you start an output buffer it basically means that any HTML you put outside of the PHP tags goes into the buffer rather than being output to the browser. I prefer this sometimes when there's a lot of different conditions and logic in the output.

ob_start, ob_get_clean

2
  • This worked great, I went with the first solution. "...so I'm not sure where the confusion was." The confusion is that I don't really fully get the code structure of PHP yet and I'm an idiot and couldn't figure this out with lots of Googling, so I owe you, Peter. Thank you!
    – Ste7en
    Commented Sep 13, 2015 at 1:24
  • That's cool, glad you figured it out! :) Commented Sep 13, 2015 at 15:39
0

$output = 'Hello World !'; mean you just assignment a value in $output variable.but when you use like
$a = 1; $output = 'Hello'; if($a == 1){ $output .= 'World'; }else{ $output .= 'Universe'; } $output .= '!'; if you print this $output then output will be same like Hello World ! because of concatenation.

so if you are not clear about (your_statement) ? 'True' : 'False' like get_post_meta( get_the_ID(), 'campus', true ) ? '<p>True so do this paragraph</p>' : '<p>false, so do this instead</p>';
you can write this instead this like
if(get_post_meta( get_the_ID(), 'campus', true )){ $output .= '<p>True so do this paragraph</p>'; }else{ $output .= <p>false, so do this instead</p>'; }

Thanks Musa

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.