How can I use a conditional statement in this PHP code?

I am trying to use a conditional statement in the code below. I am trying to make it where my WordPress home page has a dynamic header. I have succeeded there. However, I want to make it where individual pages just use their own featured image. I have tried to plug in conditional statements to make this happen and each time I break the site. Is there an easier way to accomplish my goal or some way to make the conditional statements work?

The code:

    <?php

            $recent = get_posts( array('numberposts' => 10) );
            $src = false;
            if(is_home()){
                foreach($recent as $p){
                    if( has_post_thumbnail( $p->ID ) ){
                        $src = wp_get_attachment_image_src( get_post_thumbnail_id($p->ID), array( 5600,1000 ), false, '' );
}
                $title = get_the_title();
                $date = get_the_date();
                break;

}
}    
       ?>

        <div class="hero-image" style="background-image: url('<?php echo esc_url( $src[0] ) ?>')">
          <div id="hero-text" class="thumbnail-text">
              <h1><?php echo $title?></h1>
              <h2><?php echo $date?></h2>
          </div>
        </div>

I’ve tried

if(is_home()){
result
}
else{
result
}

and tried to put it in the css, but it broke.

Read more here: How can I use a conditional statement in this PHP code?

Leave a Reply

Your email address will not be published. Required fields are marked *