-1

I'm trying to make my feature image (hero image) on WordPress to be dynamic (depends on the page). I have the following code to do it.

<?php if( has_post_thumbnail() ) { // check for feature image ?>

    <section class="feature-image" style="background: url('<?php echo $thumbnail_url; ?>') no-repeat; background-size: cover;" data-type="background" data-speed="2">
        <h1 class="page-title"><?php the_title(); ?></h1>
    </section>

    <?php } elseif ( is_page('faqs') ) { // fallback image ?>

    <section class="feature-image feature-image-faqs" data-type="background" data-speed="2">
        <h1 class="page-title"><?php the_title(); ?></h1>
    </section>

    <?php } ?>

    <?php else { // fallback image ?>

    <section class="feature-image feature-image-default" data-type="background" data-speed="2">
        <h1 class="page-title"><?php the_title(); ?></h1>
    </section>

    <?php } ?>

However, an error is prompted on this line:

<?php else { // fallback image ?>

The error goes like this:

 syntax error, unexpected T_ELSE in 

What is wrong with my code?

3
  • 3
    <?php } ?> <?php else { // fallback image ?> - If you keep that else inside same <?php ?> it should work. But I dont know the reason behind it Commented Dec 13, 2016 at 11:02
  • Thank you so much @SougataBose! It worked! Commented Dec 13, 2016 at 11:04
  • Most probably ?> is termination that control structure and thus the error appears. Commented Dec 13, 2016 at 11:17

1 Answer 1

0

There is indeed an error in your PHP.

Corrected code:

<?php if( has_post_thumbnail() ) { // check for feature image ?>

    <section class="feature-image" style="background: url('<?php echo $thumbnail_url; ?>') no-repeat; background-size: cover;" data-type="background" data-speed="2">
        <h1 class="page-title"><?php the_title(); ?></h1>
    </section>

<?php } elseif ( is_page('faqs') ) { // fallback image ?>

    <section class="feature-image feature-image-faqs" data-type="background" data-speed="2">
        <h1 class="page-title"><?php the_title(); ?></h1>
    </section>

<?php } else { // fallback image ?>

    <section class="feature-image feature-image-default" data-type="background" data-speed="2">
        <h1 class="page-title"><?php the_title(); ?></h1>
    </section>

<?php } ?>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.