WP_Query get_the_post_thumbnail not working (1 post)

  1. Evgeni Atanasov
    Posted 2 years ago #

    I am trying to make custom widget which shows the most recent post in the Project category. It should give me the post title, the thumbnail if there is any or a default image, the excerpt and link to read more of the post.
    This is my code for the widget:

    $args = array( 'posts_per_page' => 1,
                           'post_status' => 'publish',
                           'cat' => 'Projects',
                           'orderby' => 'date',
                           'order' => 'DESC',
                           'post__not_in' =>get_option('sticky_posts'));
            $query = new WP_Query( $args );
            if ( $query->have_posts() ) {
    	        while ( $query->have_posts() ) {
                    $author = get_the_author();
                    $excerpt = get_the_excerpt();
                    echo '                  <div><h5>'. get_the_title() .'</h5>
                        <a href="'. get_permalink(). '">
                        <div class="img-border">' . check_for_thumbnail() . '</div>
                        ' . string_limit_words($excerpt,20) . '
                        <a class="more" href="' . get_permalink() . '">view more</a>

    and here is my check_for_thumbnail() function code:

    function check_for_thumbnail() {
            if ( has_post_thumbnail() ) {
            } else {
                      echo '<img src="' . get_template_directory_uri() . '/images/noimage.jpg" alt="no-thumbnail-to-show"/>';

    The problem is that it does not outputs neither the thumbnail nor the default image. Also if change get_the_post_thumbnail() to the_post_thumbnail() it outputs the thumbnail of the post but no in the <div class="img-border"></div> but on the top of all the content.
    Can somebody please help me about this?

Topic Closed

This topic has been closed to new replies.

About this Topic

  • RSS feed for this topic
  • Started 2 years ago by Evgeni Atanasov
  • This topic is not resolved
  • WordPress version: 3.5.1