FreakFlag
FreakFlag

Reputation: 21

PHP if statement isn't working?

I'm working on a Wordpress index page that only shows posts if they have a featured image. This is my content.php code:

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <div class="col-sm-4">
    <?php
if (has_post_thumbnail()) {
    echo '<div class="small-index-thumbnail clear">';
    echo '<a href="' . get_permalink() . '" title="' . get_the_title() . '" rel="bookmark">';
    echo the_post_thumbnail('index-thumb');
    echo '</a>';
    echo '</div>';
}
?>
    </div>
</article><!-- #post-## -->

I'm just wondering if this is enough code for someone to tell me why it's still showing the posts, despite the posts not having a featured image. Thank you!

Upvotes: 0

Views: 56

Answers (1)

The Alpha
The Alpha

Reputation: 146191

Actually, it looks right but there is a note about this on Codex, which is something like this:

// Must be inside a loop.
if ( has_post_thumbnail() ) {
    // ...
}

Note The above code apparently fails in some instances and the below code is "recommended"

if ( '' != get_the_post_thumbnail() ) {
    // some code
} else {
    // some code
}

Check get_the_post_thumbnail if needed.

Upvotes: 1

Related Questions