Forum

Thread tagged as: Problem, Runway

PHP If Statement

Hi,

I wanted to drop in a simple PHP if statement to check if a region had content or not and then if so, to output the HTML, however it's only outputting the contents of the region and none of the HTML. Can you help?!

<?php if(perch_content('H2')!='') : ?>
            <div class="row">
                <div class="col-sm-12">
                    <h2 class="title-lg"><span><?php perch_content('H2') ?></span></h2>
                </div>
            </div> <!-- / .row -->
            <?php endif; ?>
Robin Waite

Robin Waite 0 points

  • 6 years ago

Robin Waite said:

Hi,

I wanted to drop in a simple PHP if statement to check if a region had content or not and then if so, to output the HTML, however it's only outputting the contents of the region and none of the HTML. Can you help?!

<?php if(perch_content('H2')!='') : ?>
          <div class="row">
              <div class="col-sm-12">
                  <h2 class="title-lg"><span><?php perch_content('H2') ?></span></h2>
              </div>
          </div> <!-- / .row -->
          <?php endif; ?>

Try

<?php if(perch_content('H2', true)!='') : ?>

Great, worked a treat Robert, thank you!

perch_content('region'); // echoes

perch_content('region', true); // returns

Sorry, I was too quick, it's just spitting everything out now, regardless of whether perch_content('H2', true) is actually true or not. Any further suggestions are welcome.

<?php if(perch_content('H2',true)) : ?>
            <div class="row">
                <div class="col-sm-12">
                    <h2 class="title-lg"><span><?php perch_content('H2') ?></span></h2>
                </div>
            </div> <!-- / .row -->
            <?php endif; ?>

is returning:

<div class="col-sm-12">
                    <h2 class="title-lg"><span><!-- Undefined content: H2 --></span></h2>
                </div>

Have you added data to the region named "h2".

Hi Robert, you've hit the nail on the head. The region didn't actually exist within the content therefore the conditional statement was irrelevant. I went in and saved the region with null content and it's working as expected now. So

<?php if(perch_content('H2',true)) : ?>

Is the correct conditional for what I wanted it to do. I did think about moving it into a template and using the alternative method you mentioned...that's a learning curve for another day!

Great, glad it's all working now. I'm headed off the grid now. Lol. :)