是否有条件语句可以修复我的 header?

Is there a conditional statement that would fix my header?

我已经在我的 WordPress 上设置了 header,以便在主页上完美运行。 header 应该更改为我创建的最新的 post 并且它有效。

问题是我的 header 没有出现在任何页面上。我觉得我缺少一些代码(特别是条件语句),这些代码会禁用更改并让页面具有我在仪表板上设置的特色图片。

代码:

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

我试过了

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

并试图将其放入 CSS,但它坏了。

我能够通过以下方式解决问题:

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

}
}          else if(!is_home()) {
           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();
        }
        }
   ?>