Support » Plugins » Problem with custom function (most read posts)

  • Hi,

    I created a custom function to display the most read articles in my sidebar.

    Here is the code :

    function popularPosts($num) {
        global $wpdb;
    
        $posts = $wpdb->get_results("SELECT comment_count, ID, post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , $num");
    
        foreach ($posts as $post) {
            setup_postdata($post);
            $id = $post->ID;
            $title = $post->post_title;
            $count = $post->comment_count;
    	   if ( has_post_thumbnail( $post->ID ) ){ $content = '<p>' . get_the_post_thumbnail( $post->ID, 'thumbnail'
    ) . '</p>'; }
    else {
    $content = '<p><img src="http://onirisweb.net/wp-content/uploads/2012/11/no-thumb.jpg"/></p>';
    }
    
            if ($count != 0) {
                $popular .= '<li>';
    	    $popular .=  $content;
                $popular .= '<a href="' . get_permalink($id) . '" title="' . $title . '">' . $title . '</a> ';
                $popular .= '</li>';
            }
        }
        return $popular;
    }

    You can see the demo here

    Normally I can display the featured images, but when there isn’t, then the default thumb must be displayed (question mark).

    As you can see, my problem is that some titles which don’t have featured images don’t display question mark thumb.

    Anyone could help me ?

    Thanks a lot

  • The topic ‘Problem with custom function (most read posts)’ is closed to new replies.