-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?

vishnu
  • 730
  • 2
  • 6
  • 26

1 Answers1

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 } ?>
Johann Kratzik
  • 764
  • 5
  • 11