wordpress has_post_thumbnail if/else statement not working











up vote
0
down vote

favorite












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.










share|improve this question






















  • Is this the correct path src="<?php echo get_template_directory_uri(); ?>/armco-old/assets/img/products/placeholder.jpg"? If armco-old is your theme name, try this: src="<?php echo get_template_directory_uri(); ?>/assets/img/products/placeholder.jpg"
    – Jake Stewart
    Nov 22 at 17:16












  • Yes, as mentioned when removing the if statement and only using the echo image it works fine.
    – Daniel Vickers
    Nov 22 at 17:16










  • Is the console giving you any error messages?
    – Jake Stewart
    Nov 22 at 17:18










  • It says this in the codex: "It’s worth noting that has_post_thumbnail() does not just check for the Featured Image as the Codex User Contributed Note suggests. If a post contains no defined featured image but it does contain an image in the content this function will still return TRUE." Are there any photos in the posts that do not contain featured images?
    – Jake Stewart
    Nov 22 at 17:25










  • None of these contain images in the content
    – Daniel Vickers
    Nov 22 at 17:53















up vote
0
down vote

favorite












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.










share|improve this question






















  • Is this the correct path src="<?php echo get_template_directory_uri(); ?>/armco-old/assets/img/products/placeholder.jpg"? If armco-old is your theme name, try this: src="<?php echo get_template_directory_uri(); ?>/assets/img/products/placeholder.jpg"
    – Jake Stewart
    Nov 22 at 17:16












  • Yes, as mentioned when removing the if statement and only using the echo image it works fine.
    – Daniel Vickers
    Nov 22 at 17:16










  • Is the console giving you any error messages?
    – Jake Stewart
    Nov 22 at 17:18










  • It says this in the codex: "It’s worth noting that has_post_thumbnail() does not just check for the Featured Image as the Codex User Contributed Note suggests. If a post contains no defined featured image but it does contain an image in the content this function will still return TRUE." Are there any photos in the posts that do not contain featured images?
    – Jake Stewart
    Nov 22 at 17:25










  • None of these contain images in the content
    – Daniel Vickers
    Nov 22 at 17:53













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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.










share|improve this question













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.







php wordpress






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 at 16:18









Daniel Vickers

354215




354215












  • Is this the correct path src="<?php echo get_template_directory_uri(); ?>/armco-old/assets/img/products/placeholder.jpg"? If armco-old is your theme name, try this: src="<?php echo get_template_directory_uri(); ?>/assets/img/products/placeholder.jpg"
    – Jake Stewart
    Nov 22 at 17:16












  • Yes, as mentioned when removing the if statement and only using the echo image it works fine.
    – Daniel Vickers
    Nov 22 at 17:16










  • Is the console giving you any error messages?
    – Jake Stewart
    Nov 22 at 17:18










  • It says this in the codex: "It’s worth noting that has_post_thumbnail() does not just check for the Featured Image as the Codex User Contributed Note suggests. If a post contains no defined featured image but it does contain an image in the content this function will still return TRUE." Are there any photos in the posts that do not contain featured images?
    – Jake Stewart
    Nov 22 at 17:25










  • None of these contain images in the content
    – Daniel Vickers
    Nov 22 at 17:53


















  • Is this the correct path src="<?php echo get_template_directory_uri(); ?>/armco-old/assets/img/products/placeholder.jpg"? If armco-old is your theme name, try this: src="<?php echo get_template_directory_uri(); ?>/assets/img/products/placeholder.jpg"
    – Jake Stewart
    Nov 22 at 17:16












  • Yes, as mentioned when removing the if statement and only using the echo image it works fine.
    – Daniel Vickers
    Nov 22 at 17:16










  • Is the console giving you any error messages?
    – Jake Stewart
    Nov 22 at 17:18










  • It says this in the codex: "It’s worth noting that has_post_thumbnail() does not just check for the Featured Image as the Codex User Contributed Note suggests. If a post contains no defined featured image but it does contain an image in the content this function will still return TRUE." Are there any photos in the posts that do not contain featured images?
    – Jake Stewart
    Nov 22 at 17:25










  • None of these contain images in the content
    – Daniel Vickers
    Nov 22 at 17:53
















Is this the correct path src="<?php echo get_template_directory_uri(); ?>/armco-old/assets/img/products/placeholder.jpg"? If armco-old is your theme name, try this: src="<?php echo get_template_directory_uri(); ?>/assets/img/products/placeholder.jpg"
– Jake Stewart
Nov 22 at 17:16






Is this the correct path src="<?php echo get_template_directory_uri(); ?>/armco-old/assets/img/products/placeholder.jpg"? If armco-old is your theme name, try this: src="<?php echo get_template_directory_uri(); ?>/assets/img/products/placeholder.jpg"
– Jake Stewart
Nov 22 at 17:16














Yes, as mentioned when removing the if statement and only using the echo image it works fine.
– Daniel Vickers
Nov 22 at 17:16




Yes, as mentioned when removing the if statement and only using the echo image it works fine.
– Daniel Vickers
Nov 22 at 17:16












Is the console giving you any error messages?
– Jake Stewart
Nov 22 at 17:18




Is the console giving you any error messages?
– Jake Stewart
Nov 22 at 17:18












It says this in the codex: "It’s worth noting that has_post_thumbnail() does not just check for the Featured Image as the Codex User Contributed Note suggests. If a post contains no defined featured image but it does contain an image in the content this function will still return TRUE." Are there any photos in the posts that do not contain featured images?
– Jake Stewart
Nov 22 at 17:25




It says this in the codex: "It’s worth noting that has_post_thumbnail() does not just check for the Featured Image as the Codex User Contributed Note suggests. If a post contains no defined featured image but it does contain an image in the content this function will still return TRUE." Are there any photos in the posts that do not contain featured images?
– Jake Stewart
Nov 22 at 17:25












None of these contain images in the content
– Daniel Vickers
Nov 22 at 17:53




None of these contain images in the content
– Daniel Vickers
Nov 22 at 17:53












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










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 } ?>





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434893%2fwordpress-has-post-thumbnail-if-else-statement-not-working%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    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 } ?>





    share|improve this answer

























      up vote
      0
      down vote



      accepted










      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 } ?>





      share|improve this answer























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        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 } ?>





        share|improve this answer












        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 } ?>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 at 18:44









        Daniel Vickers

        354215




        354215






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434893%2fwordpress-has-post-thumbnail-if-else-statement-not-working%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            What visual should I use to simply compare current year value vs last year in Power BI desktop

            Alexandru Averescu

            Trompette piccolo