Wordpress Fallback image

120 views Asked by At

I have picked up a project from a previous developer and he is using this code to generate thumbnails. I need to add a default fallback image and I'm unsure of the best practice.

<?php get_header(); ?>
<?php get_sidebar(); ?>
        <div id="container">
        <?php if(have_posts()) : ?>
        <?php while(have_posts()) : the_post(); ?>
        <div class="post" id="post-<?php the_ID(); ?>">
            <div class="entry">
                <?php the_content(); ?>
            </div>
            <?php $categories = get_categories(array('orderby'=>'order','number'=>'13'));
            $wud = wp_upload_dir();
            $width = get_option('thumbnail_size_w');
            $height = get_option('thumbnail_size_h');
            $exclude_cats = array(1, 27); // Array of category ID's to exlude from this page
            ?>
            <div id="gallery">
                <ul class="cat-list">
                <?php foreach ($categories as $cat) {
                    if (!in_array($cat->cat_ID, $exclude_cats)) : ?>
                    <li class="cat-item cat-item-<?php echo $cat->cat_ID; ?>">
                        <a href="<?php echo get_category_link( $cat->term_id ); ?>" title="View all artists under the <?php echo $cat->cat_name; ?> category">
                            <div class="cat-ss" style="position: relative; width: <?php echo $width; ?>px; height: <?php echo $height; ?>px; overflow: hidden;">
                            <?php $artists = get_posts('category=' .  $cat->cat_ID . '&orderby=rand');
                            foreach ($artists as $artist) {
                            $name = get_the_title( $artist->ID );
                            $thumb = $wud['baseurl'] . '/thumb-' . sanitize_title( $name ) . '-' . $width . 'x' . $height . '.jpg'; ?>

                                <img src="<?php echo $thumb; ?>" alt="<?php echo $name; ?>" title="<?php echo $name; ?>" width="<?php echo $width; ?>" class="cat-ss-image" height="<?php echo $height; ?>" style="position: absolute; top: 0; left: 0;" />
                            <?php } ?>
                                <div class="cat-ss-caption png" style="position: absolute; bottom: 0;"><h3><?php echo $cat->cat_name; ?></h3></div>

                            </div>
                        </a>
                    </li>
                <?php endif;
                } ?>
                <ul>
            </div>
            <div style="clear: both"></div>
        </div>
        <?php endwhile; ?>
        <?php else : ?>
        <div class="post">
            <h2><?php _e('Not Found'); ?></h2>
        </div>
        <?php endif; ?>
        </div>
        </div>
<?php get_footer(); ?>
2

There are 2 answers

0
Aibrean On

Looks like his code to pull in a thumb is

<?php $thumb = $wud['baseurl'] . '/thumb-' . sanitize_title( $name ) . '-' . $width . 'x' . $height . '.jpg'; ?>

So to make it a conditional if/else statement if it's not empty render the image thumbnail, and use a fallback if it is empty, do this (add whatever other code you need to for the image to render):

<?php if(!empty $thumb) { ?>
<img src="<?php echo $thumb; ?>" alt="<?php echo $name; ?>" title="<?php echo $name; ?>" width="<?php echo $width; ?>" class="cat-ss-image" height="<?php echo $height; ?>" style="position: absolute; top: 0; left: 0;" />
<?php } else { ?>
<img src="yourfallback.jpg" style="position: absolute; top: 0; left: 0;">
<?php } ?>
0
flomei On

Regardless of your code you should have a look into has_post_thumbnail and then use get_the_post_thumbnail to output a thumbnail or your fallback image.

Works like a charm on many of my sites. You can find examples in the Codex pages I linked to.