3

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

1
  • there is an syntax error .. php tags are not been closed, turn error reporting on Commented Dec 18, 2013 at 9:26

4 Answers 4

2

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"]' );
}
?>
Sign up to request clarification or add additional context in comments.

1 Comment

We all have to start somewhere don't we? Glad i could help!
2

You are using html in between php . So either use opening and closing php <?php... ?> or if single html line then you can just use echo as below

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

Comments

2

The other answers are right, but here's another syntax you can use when you mix PHP and HTML (which I prefer as I find it more readable):

<?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"]'); ?>
<?php 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"]'); ?>
<?php else: ?>
    <h2 id="formhead">Buy, Hire or Enquire</h2>
    <?php echo do_shortcode('[gravityform id="1" name="Contact Form" title="false" description="false"]'); ?>
<?php endif; ?>

Comments

1
<?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"]' );
}
?>

That might work (untested)

You weren't opening and closing your PHP tags and your brackets were a bit off.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.