• I have the following code that display thumbnail and excerpt in the homepage. It will auto retrieve the first image in a post and convert to a thumbnail. However, I don’t know how to hide the thumbnail box if there’s no image in a post. Currently it will show empty box because there’s no image in the post and thus, no thumbnail generated.

    Hoping someone could let me know how to solve the problem

    Thank you very much

    <div class=”boxitem”>
    <h5>” ><?php the_title() ?></h5>
    <span class=”itemdets”>
    Posted On: “><?php the_time(‘j F Y’); ?>
    </span>
    <div class=”thumbnail”>

    ” title=”<?php the_title_attribute(); ?>”>

    <img src=”<?php bloginfo(‘template_directory’); ?>/timthumb.php?src=<?php getImage(‘1’); ?>&w=120&h=120&zc=1″>

    </div>
    <?php the_content_rss(”, TRUE, ”, 25); ?>
    <span class=”morelink”>
    “>Read More…
    </span>
    </div>

Viewing 2 replies - 1 through 2 (of 2 total)
  • Check the timthumb documentation to see if there is a call to check if a thumbnail exists, if not you need to return the filename and test.

    This is untested code and should not be tested on a live website, use a local install.

    You just need to add in a condition if the file exists.

    <?php /* Start Added conditional code */ ?>
    <?php $thumbsrc = bloginfo('template_directory') .'/timthumb.php?src=' .getImage('1'); ?>
    <?php if(file_exists($thumbsrc)) : ?>
        <div class="thumbnail">
           " title="<?php the_title_attribute(); ?>">
           <img src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<?php getImage('1'); ?>&w=120&h=120&zc=1">
        </div>
    <?php endif; ?>
    <?php /* End Added conditional code */ ?>

    HTH

    David

    Hi David,

    I am trying to use the code your prescribed to hide a thumbnail from a frontpage blog/excerpt post and I have been unsuccesful. 🙂 Here is the code I am trying to manipulate to get my website that if there is not an image in a post, it will not show the thumbnail box.

    Thanks for any help. ~W

    [code moderated as per forum rules - please use the pastebin]

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Hide box if no thumbnail’ is closed to new replies.