1

I've been trying to develop a recent posts widget which will display my most recent posts in sidebar. I am trying to display default "ft-thumb.jpg" image if there is no post thumbnail but its not working. Here is my code -

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

  • 2
    Please explain "it's not working". Many issues here, e.g. you're not closing the tag: <img src=$url – birgire Feb 11 '16 at 8:47
  • } else {echo "<img src=$url>";} – majick Feb 11 '16 at 10:49
  • sorry it's just error in copying code, I am closing the tag still its not working.. I am getting above result like in screenshot. – Raunak Hajela Feb 11 '16 at 11:29
  • You need to take another screenshot now that you have closed the tag, so we can see the new error. – Kaspar Lee Feb 11 '16 at 12:07
  • I checked its same as before. – Raunak Hajela Feb 11 '16 at 12:13
0

You're not quoting your HTML attributes - one section should be:

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' >";
}

...the other should be:

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>";
  • else { echo "<img src='$url' >";} only this condition not working, rest is all fine.. – Raunak Hajela Feb 11 '16 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.