0

I want to show a default thumbnail if a custom post not created yet. Here is my loop:

<?php  while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<?php  
   if ( has_post_thumbnail( ) ) {
       the_post_thumbnail( 'full', array( 'class' => 'mySlides' ) ); 
    }
    else {
        echo '<img src="dummy-image-1-1.jpg" class="mySlides" height="350px"/>';
        }
    ?>
    <?php
       endwhile;
      wp_reset_postdata(); 

    ?>

If I created a post but not posted a thumbnail then show default thumbnail. But If I didn't create a post yet I want to show a default thumbnail. How to fix it?

  • 1
    I don't understand your question. "If I didn't create a post" -> if you didn't create a post, then there is nothing in the loop. How do you know whether something wasn't created yet? – kero Mar 26 at 10:44
  • I already told it in my question. If I created a post but not upload image then it shows default image like echo else But If I didn't created a post I want to show a default default Like if post not exists yet then show default – Sabbir Mar 26 at 10:59
3

Create "images" folder in current active theme the put "dummy-image-1-1.jpg" in that folder.

<?php  
if ($the_query->have_posts() ) :
    while ($the_query->have_posts()) : $the_query->the_post(); 
        if ( has_post_thumbnail( ) ) {
            the_post_thumbnail( 'full', array( 'class' => 'mySlides' ) ); 
        } else {
        ?>
            <img src="<?php echo get_template_directory_uri().'/images/dummy-image-1-1.jpg'; ?>" class="mySlides" height="350px"/>
        <?php
        }
    endwhile;
else:
?>
    <img src="<?php echo get_template_directory_uri().'/images/dummy-image-1-1.jpg'; ?>" class="mySlides" height="350px"/>
<?php
endif;
wp_reset_postdata(); 
?>

Use path according to the theme(parent or child)

  • brother my question is If I created a post but not upload image then it shows default image like echo else But If I didn't created a post I want to show a default default Like if post not exists yet then show default – Sabbir Mar 26 at 10:59
  • I already get the image so it doesn't matter just want to show if post to exists yet. – Sabbir Mar 26 at 10:59
  • @Sabbir I have updated code...Try it please... – Tanmay Patel Mar 26 at 11:04
  • It works and I got it. Actually, I forgot the method Thanks @Tanmay – Sabbir Mar 26 at 11:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.