Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP If, Elseif, Else Statement in Wordpress Header

Tags:

php

wordpress

I have an H2 and a form at the top of my page, it is present on ever page and is in the Theme template, in header.php, but I need the content to change on 2 specific pages. The current code is this:

<h2 id="formhead">Buy, Hire or Enquire</h2>
<?php echo do_shortcode( '[gravityform id="1" name="Contact Form" title="false" description="false"]' ); ?>

I tried changing it to an if, elseif, else statement:

<?php
if (is_page('54')) {
<h2 id="formhead">Hire or Enquire</h2>
echo do_shortcode( '[gravityform id="3" name="Contact Form" title="false" description="false"]' );
} elseif (is_page('52')) {
<h2 id="formhead">Buy or Enquire</h2>
echo do_shortcode( '[gravityform id="2" name="Contact Form" title="false" description="false"]' );
} else {
<h2 id="formhead">Buy, Hire or Enquire</h2>
echo do_shortcode( '[gravityform id="1" name="Contact Form" title="false" description="false"]' );
}
?>

This however, breaks the site completely and nothing loads. I have no real php experiance(I'm sure there is just a very basic error in the code), only what I've figured out by trial and error, but since this is going on a live site, I can't afford to constantly break the site.

Thanks in advance
Willem

like image 231
Willem Avatar asked May 24 '26 17:05

Willem


1 Answers

You have to open and close the php tags:

<?php
if (is_page('54')) { ?>
<h2 id="formhead">Hire or Enquire</h2>
<?php echo do_shortcode( '[gravityform id="3" name="Contact Form" title="false" description="false"]' );
} elseif (is_page('52')) { ?>
<h2 id="formhead">Buy or Enquire</h2>
<?php echo do_shortcode( '[gravityform id="2" name="Contact Form" title="false" description="false"]' );
} else { ?>
<h2 id="formhead">Buy, Hire or Enquire</h2> <?php
echo do_shortcode( '[gravityform id="1" name="Contact Form" title="false" description="false"]' );
}
?>
like image 185
veelen Avatar answered May 26 '26 06:05

veelen