在php While循环中使用IF

斯旺·纳米克(Shwan Namiq)

我下面有这段代码是在wordpress中获得热门帖子的循环

$First_Img = '<img src="'.YPE_Catch_First_Image().'">';
while ( $popular->have_posts() ) : $popular->the_post();
    $html  = '<article>';
    $html .= '<section class="bootstrap-nav-thumb">';
    $html .= '<p>';
    $html .= '<a href="' . get_permalink() . '">';
    $html .= get_the_post_thumbnail(get_the_ID(), array('class' => 'img-responsive'));
    $html .= '</a>';
    $html .= '</p>';
    $html .= '</section>';

    $html .= '<aside class="bootstrap-title-info">';
    $html .= '<p>';
    $html .= '<a href="' . get_permalink() . '">'.get_the_title().'</a>';
    $html .= '</p>';
    $html .= '<p class="text-muted">' . get_the_date() . '||'. getPostViews(get_the_ID()) . '</p>';
    $html .= '</aside>';
    $html .= '</article>';
    echo $html;
endwhile;

我想使用此代码

html .= if(has_post_thumbnail()) { 
    echo get_the_post_thumbnail(get_the_ID(), array('class' => 'img-responsive'));
} else {
    echo $First_Img;
};

代替此代码

$html .= get_the_post_thumbnail(get_the_ID(), array('class' => 'img-responsive'));

但是服务器显示错误,unexpected 'if' (T-IF)请帮助我在发布缩略图后如何使用条件语句,如果没有在缩略图中打印第一张图片,该如何使用条件语句?

gpupo

不正确:

$html .= if(has_post_thumbnail()) { 
    echo get_the_post_thumbnail(get_the_ID(), array('class' => 'img-responsive'));
} else {
    echo $First_Img;
};

正确的:

if(has_post_thumbnail()) { 
    $html .=  get_the_post_thumbnail(get_the_ID(), array('class' => 'img-responsive'));
} else {
    $html .=  $First_Img;
};

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章