1

I am using the following code to print another image that is in my images folder in casa a post has no thumbnail but it is giving me errors on the else statement saying that there is a syntax error:

<?php if ( has_post_thumbnail() ) {
       echo '<a href="<?php the_permalink(); ?>"> <figure><?php the_post_thumbnail(); ?></figure></a>';
    }
    else {
        echo '<figure><img src="<?php echo get_bloginfo( 'template_directory' ); ?>/images/stone.jpg" /></figure></a>';
    }
?>

However, if I paste this code:

<?php if ( has_post_thumbnail() ) {
      echo '<a href="<?php the_permalink(); ?>"> <figure><?php the_post_thumbnail(); ?></figure></a>';
   }
   else{
   }
?>

It gives no error but also it does not display the thumbnail

Hope you can help

  • what are the errors? plz mention – Arsalan Mithani Feb 23 '17 at 10:44
  • What error do you get ..please let us know,so we can help you – Sonali Feb 23 '17 at 10:53
  • @Arsalan Mithani. Please check my edited question. It shows nothing now that I removed the else statement code. – user agent Feb 23 '17 at 10:54
1

Try this inside else condition where no image assigned.

    if (has_post_thumbnail()) {
    ?><a href="<?php the_post_thumbnail_url(); ?>">
    <?php the_post_thumbnail();?>
    </a><?php
} else {
    echo '<figure><a href="add_link_here"><img src="'.get_bloginfo("stylesheet_directory").'/images/stone.jpg" /></figure></a>';
}
| improve this answer | |
  • I actually found the answer. Mind making it better by showing how can I wrap the thumbnail in a permalink? – user agent Feb 23 '17 at 11:01
  • Do you need to add permalink in image? – Sonali Feb 23 '17 at 11:03
  • Yes. Need the thumbnail to be wrapped in a permalink – user agent Feb 23 '17 at 11:05
  • See my updated answer.Do you need permalink in if condition or only else condition?Because added it only for else condition here. – Sonali Feb 23 '17 at 11:11
  • Not really that. Looking at my answer, the function the_post_thumbnail(); should be wrapped in an a tag using the permalink. – user agent Feb 23 '17 at 11:14
0

I found a snippet from codex and it actually works.

<?php
    // Must be inside a loop.

    if ( has_post_thumbnail() ) {
        the_post_thumbnail();
    }
    else {
        echo '<img src="' . get_bloginfo( 'stylesheet_directory' ) 
            . '/images/stone.jpg" />';
    }
?>                      
| improve this answer | |

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.