使用HAS_POST_THMBILING时ELSE条件不起作用

时间:2016-02-11 作者:Raunak Hajela

我一直在尝试开发一个“最近发表的文章”小部件,它将在侧边栏中显示我最近发表的文章。我试图显示默认的“ft thumb.jpg”图像,如果没有帖子缩略图,但它不工作。这是我的代码-

public function widget($args,$instance) {
        //displays our widget
        extract($args);
        extract($instance);
        $number = $instance[\'number\'];
        echo $before_widget;
           if(!$title) {
            echo \'<div class="widget-title"><h2>Recent Posts</h2></div>\';
           } 
           else {
            echo $before_title .$title. $after_title;
           }
           $popular_posts = new WP_Query(array(\'posts_per_page\' => $number,\'post__not_in\' => get_option( \'sticky_posts\' ),\'orderby\' => \'desc\'));
           if($popular_posts->have_posts()) {
            while($popular_posts->have_posts()) {
                $popular_posts->the_post(); global $post;
                $link = get_permalink();
                $url = get_bloginfo( \'template_directory\' )."/images/ft-thumb.jpg";
                echo "<div class=\'row\'>" . "<div class=\'col-md-12  widget-recent-post\'>";

                    echo "<div class=\'rc-post-image\'>";
                    if( has_post_thumbnail() ) {
                        $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID));
                        echo "<a href=$link><img src=$image[0]" . " width=\'98\' height=\'80\' class=\'img-responsive\' /></a>";
                    } else {echo "<img src=$url >";}

                    echo "</div>";

                    echo "<div class=\'rc-post-area\'>";
                        echo "<h3><a href=$link>";
                        the_title(); 
                        echo "</a></h3>";
                        echo "<div class=\'post-meta\'><span class=\'date-meta\'>On <a href=$link>";
                        the_time(get_option(\'date_format\'));
                        echo "</a></span></div>";
                    echo "</div>";

                echo "</div></div>";
            }
           }
        echo $after_widget;
    } 

enter image description here

1 个回复
最合适的回答,由SO网友:TheDeadMedic 整理而成

您没有引用HTML属性-其中一个部分应该是:

if ( has_post_thumbnail() ) {
    $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID));
    echo "<a href=\'$link\'><img src=\'$image[0]\' width=\'98\' height=\'80\' class=\'img-responsive\' /></a>";
} else {
    echo "<img src=\'$url\' >";
}
。。。另一个应该是:

echo "<h3><a href=\'$link\'>";
the_title(); 
echo "</a></h3>";
echo "<div class=\'post-meta\'><span class=\'date-meta\'>On <a href=\'$link\'>";
the_time( get_option( \'date_format\' ) );
echo "</a></span></div>";

相关推荐