Daniel Vickers
Daniel Vickers

Reputation: 1074

wordpress has_post_thumbnail if/else statement not working

I have the following code, it seems to ignore the if/else for the displaying the thumbnail. It will display all thumbnails should a post have a thumbnail set however it will not show the placeholder if there is no thumbnail. Am I blind or is there something wrong with this?

<?php $x = 0;
    $displayed = [];

    function runQuery($args) {
        global $displayed;
        global $x;
        $query = new WP_Query( $args );

        if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
            $cat_terms = get_the_terms($post->id, 'product_cat');
            $datagroups = '';

            if ( in_array( get_the_ID(), $displayed ) ){
                continue;
            }
            // update array with currently displayed post ID
            $displayed[] = get_the_ID();

            foreach ($cat_terms as $key => $cat) {
                if (count($cat_terms) == ($key + 1)) {
                    $datagroups .= '"' . $cat->name . '"';
                } else {
                    $datagroups .= '"' . $cat->name . '", ';
                }
            }
        ?>      

        <div class="flex-item product-single flex-cols-4" data-groups='[<?php echo $datagroups; ?>]' data-date-created="<?php the_modified_date('Y-m-d') ?>" data-title="<?php the_title() ?>">
            <figure class="picture-item__inner">
                <figcaption class="picture-item__title">
                    <a data-open="product-<?php echo $x; ?>">
                        <div class="product-wrap">
                            <div class="product-image">
                                <?php if( has_post_thumbnail() ) { ?>
                                    <?php the_post_thumbnail('medium');?>
                                <?php } else { ?>
                                    <img src="<?php echo get_template_directory_uri(); ?>/armco-old/assets/img/products/placeholder.jpg" alt="Coming Soon" />
                                <?php } ?>
                            </div>
                            <div class="product-title">
                                <p><?php the_title(); ?></p>
                            </div>
                        </div>
                    </a>
                </figcaption>
            </figure>
        </div>
        <?php $x ++;
        endwhile;
        endif;
        wp_reset_postdata();
    }?>
    <?php if ( $terms && !is_wp_error( $terms ) ) {

        foreach ( $terms as $term ) {

            $args = array(
                'post_type'  => 'products',
                'posts_per_page' => -1,
                'orderby' => 'menu_order',
                'tax_query' => array(
                    array(
                        'taxonomy' => 'product_cat',
                        'field'    => 'slug',
                        'terms'    => $term->slug,
                    ),
                ),
                'order' => 'asc',
            );

            runQuery($args);
        }
    } ?>
</div>

Any help would be appreciated as I cannot see what the issue is, when I remove the first if statement and just tell it to echo out the image it works with brining in the image however as soon as it's inside the else statement it doesn't work.

Upvotes: 0

Views: 3764

Answers (1)

Daniel Vickers
Daniel Vickers

Reputation: 1074

Due to whatever reasoning my original code did not work, so I swapped it to check if the get_the_post_thumbnail() function was empty or not:

<?php if( !empty(get_the_post_thumbnail()) ) { ?>
    <?php the_post_thumbnail('medium');?>
<?php } else { ?>
    <img src="<?php echo get_template_directory_uri(); ?>/armco-old/assets/img/products/placeholder.jpg" alt="Coming Soon" />
<?php } ?>

Upvotes: 3

Related Questions