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

时间:2017-04-15 01:29:59

标签: php wordpress conditional

我已经在我的WordPress上设置了标题,以便在主页上精彩地工作。标题应该更改为我创建的最新帖子并且它有效。

问题是我的标题没有显示在任何页面上。我觉得我缺少一些代码(特别是一个条件语句)会禁用更改,只是让页面具有我在仪表板上设置的特色图像。

代码:

<?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中,但它破了。

1 个答案:

答案 0 :(得分:0)

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

<?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();
        }
        }
   ?>
相关问题