• I am currently creating a website based on a tutorial and have pasted in a script within the loop which generates the post contents. The script takes a featured image from a post and then inserts that into the front end. If the post doesn’t have a featured image then there is a path to a default image within the else statement of this conditional script that is supposed to be included.

    Unfortunately it doesnt include this fallback after the first post and i cant figure out why? Any suggestions? Here is the site link.
    http://sandpit.jonathanbeech.co.uk/

    <?php if ( function_exists('has_post_thumbnail') && has_post_thumbnail($post->ID) ) {
    
    $thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );
    
    ?>
    
    		<img src="<?php echo $thumbnail[0]; ?>" alt="" />
    
    		<?php }else{ ?>	
    
    			<img src="<?php echo get_template_directory_uri(); ?>/images/thumb_iceland.jpg" alt="iceland" />
    
    		<?php } ?>
Viewing 11 replies - 1 through 11 (of 11 total)
  • Hi,

    I saw the source code of your home page gives this for the featured image where your code is not worling :
    <img src="" alt="">
    This means the static code is correct but your function is not correct or not applicable to your theme.

    Instead of open and close PHP tag each time you use PHP around HTML code, you should try to use the ‘echo’ command within the PHP code. This prevent PHP tag issue and limit the number of PHP instances.

    Also, instead of to get only the thumbnail source URL, try to get the entire thumbnail structure : <img src=”xxxxx” alt=”yyyyy” title=”zzzzz”> with the WordPress function ‘the_post_thumbnail’.

    Try with this code :

    <?php if (has_post_thumbnail()) {
      echo '<div class="attachment-thumbnail">';
      the_post_thumbnail('full', array('title' => trim(strip_tags( $attachment->post_title ))));
      echo '</div>';
    }
      else
    {
      echo '<img src="<?php echo get_template_directory_uri(); ?>/images/thumb_iceland.jpg" alt="iceland" />';
    }
    ?>
    Thread Starter Jonathan Beech

    (@jonathan-beech)

    Many thanks for your input, I will keep the points you made about writing php in the manner you describe as good practice.

    I tried your code and the default fall back image does not show even on the first post that is outputted. Also the code must be echoing out the dimensions of the original image since it is stretched vertically. If you have any thoughts I would be very pleased to hear them?

    To display the thumbnail instead of the full width image, just replace :
    the_post_thumbnail('full', array('title' => trim(strip_tags( $attachment->post_title ))));
    by
    the_post_thumbnail('thumbnail', array('title' => trim(strip_tags( $attachment->post_title ))));
    in the code I sent you above.

    Thread Starter Jonathan Beech

    (@jonathan-beech)

    Thats looking a lot better many thanks.

    However I dont see any default fallback images coming in still. Any thoughts?

    http://sandpit.jonathanbeech.co.uk/

    … you certainly have an issue with the loop.
    But it’s strange the 2 first posts displays the thumbnail correctly.
    If it was a loop issue, there would be only 1 correctly displayed post.

    But did you defined a thumbnail (featured image) for the other posts ?

    Thread Starter Jonathan Beech

    (@jonathan-beech)

    Just to confirm there is no featured image in the first post at the top of the page, however the fallback landscape image described in the script appears just fine. The second post does have a featured image ( the motorbike ) but in answer to your question the rest of the posts do not.

    The subsequent posts are supposed to have the fallback image in absence of the featured image according to the script. This fallback image doesn’t show! This is strange to me too. Many thanks for your input on this.

    Hi,

    Can I have a look to your PHP template file ?
    Send it here : https://www.wetransfer.com/ and paste here the link to allow me to download/read the file.

    Thread Starter Jonathan Beech

    (@jonathan-beech)

    Thats really generous of you!

    Unfortunately I’m not a member of wetransfer. Would I need to sign up to their account? I have sent you an email on your website so I could hopefully get a reply from you and then I could send the email as a zip file directly to you using the free portion of the wetransfer service?

    No, sign-up is not required.

    I replied to you from my own mailbox.
    Send me the file by email.

    Thread Starter Jonathan Beech

    (@jonathan-beech)

    Much appreciated

    Check your emails 😉

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘default thumbnail image not showing’ is closed to new replies.