Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I need to modify the following code to show a placeholder if there is no featured image available.

I understand that I need to add a if statement in the following code but wasn't sure of the exact coding I needed. Obviously in plain english it's going to be along the lines of if there is a thumbnail show it, if not show the placeholder.

<?php $rel = $related->show(get_the_ID(), true);
     $count = 1;
    foreach ($rel as $r) {
     $class= ($count%6 == 0)?"category-related-item-right":"";

     echo '<div class="category-related-item '.$class.'"><a href='.get_permalink($r->ID).'>'.'<div class=category-related-title>'.$r->post_title.'</div>'.get_the_post_thumbnail($r->ID, array(50,50)).'</a></div>';
$count++;
}?>

I've tried the following but something isn't quite right as it's breaking the page...

<?php $rel = $related->show(get_the_ID(), true);
$count = 1;
foreach ($rel as $r) {
$class= ($count%6 == 0)?"category-related-item-right":"";

echo '<div class="category-related-item '.$class.'"><a href='.get_permalink($r->ID).'>'.'<div class=category-related-title>'.$r->post_title.'</div>';
if (get_the_post_thumnail($r->ID)) : 
get_the_post_thumbnail($r->ID, array(50,50));
else :
echo '<img src="image_url"/>';
endif;
echo '</a></div>';
$count++;
}?>
share|improve this question
add comment

3 Answers

up vote 4 down vote accepted

Misspelling of thumbnail (thumnail) in the if statement.

Also get_the_post_thumbnail does echo the thumbnail, but just returns the html. You need to echo it.

Also, for checking if a post has a thumbnail, you can use has_post_thumbnail.

 if ( has_post_thumbnail($r->ID)) {
    echo get_the_post_thumbnail($r->ID, array(50,50));
 }else{
    echo '<img src="image_url"/>';
 }
share|improve this answer
    
Thanks... the smallest of things are always the cause of a headache! –  Vince Pettit Mar 2 '12 at 12:26
add comment

For designers who have less or no knowlege and for staters in wordpress to get image code is given here. Designers can just copy wordpress get image code and paste.

if ( has_post_thumbnail($post->ID) ){   
    $image = wp_get_attachment_image_src(get_post_thumbnail_id(), 'full');
    $image = $image[0];
    echo '<div id="product" class="MagicZoomPlus" href="'.$image.'" rel="selectors-class: Active"><img src="'.$image.'" alt="" /></div>';
}else{  
    $image = get_template_directory_uri() .'/img/placeholder-580.png'; 
    echo '<a id="product" class="MagicZoomPlus" href="'.$image.'" rel="selectors-class: Active"><img src="'.$image.'" alt="" /></a>';
}
share|improve this answer
add comment

A function to add default images for themes

The following code can also be found on Trac as Ticket 1).

1) Please add yourself as @cc there to support it. Thanks!

Ticket Link

Using the function wp_default_img():

It works with an input array of attributes and offers two filters (wp_default_img_attr & wp_default_img). So setting up default images is as easy as using a filter (if the theme developer isn't satisfied with the functions default args) and finally just adding…

// functions.php during init:
add_image_size( 'default_img', 80, 80, true );

// Inside some template
$placeholder = get_site_url( null, 'your_path' ).'/some_img.jpg';
echo wp_default_img( array( 'url' => $placeholder, 'size' => 'default_img' ) );

The function also cares about cropping images if 4th argument set to true when registering the size using add_image_size();.

function wp_default_img( $attr )
{
        // Sizes registered via add_image_size();
        global $_wp_additional_image_sizes;

        $defaults = array(
             'size'     => 'medium'
            ,'classes'  => false
            ,'alt'      => false
            ,'title'    => false
            ,'align'    => 'none'
            ,'echo'     => true 
        );

        $attr = wp_parse_args( $attr, $defaults );

        if ( 'thumb' === $attr['size'] )
            $attr['size'] = 'thumbnail';

        // Size in built in sizes - call size setting from DB
        # behavoir in here, dependent on outcome of @link http://core.trac.wordpress.org/ticket/18947
        if ( ! in_array( $attr['size'], array_keys( $_wp_additional_image_sizes ) ) )
        {
            $sizes                        = get_intermediate_image_sizes();
            // Get option - gladly autoloaded/can use wp_cache_get();
            $size_data['width']  = intval( get_option( "{$attr['size']}_size_w") );
            $size_data['height']= intval( get_option( "{$attr['size']}_size_h") );
            // Not sure how this will behave if cropped is false (autoloaded option not added)
            $size_data['crop']    = get_option( "{$attr['size']}_crop" ) ? get_option( "{$attr['size']}_crop" ) : false;
        }
        // Size array from global registered additional/custom sizes array
        else 
        {
            $size_data = $_wp_additional_image_sizes[ $attr['size'] ];
        }

        // Retrieve image width & height
        $img_info       = @getimagesize( $attr['url'] );

        // Calculate final dimensions - if "crop" was set to true during add_image_size(), the img will get ... cropped
        $end_sizes      = image_resize_dimensions( $img_info[0], $img_info[1], $size_data['width'], $size_data['height'], $size_data['crop'] );

        // defaults to px units - can't get changed, as applying units is not possible
        $hwstring       = ' '.trim( image_hwstring( $end_sizes[4], $end_sizes[5] ) );

        // Attributes:
        // Not made required as users tend to do funky things (...and lock screen readers out)
        $attr['alt'] = $attr['alt'] ? ' alt="'.esc_attr( $attr['alt'] ).'"' : '';

        if ( ! $attr['title'] )
        {
            $mime = explode( "/", $img_info['mime'] );
            $attr['title'] = sprintf( __('default image of type: %1$s'), ucfirst( $mime[1] ) );
        }
        $attr['title'] = $attr['title'] ? ' title="'.esc_attr( $attr['title'] ).'"' : '';

        $attr['classes'] = "wp-img-default ".esc_attr( $attr['classes'] ? $attr['classes'] : '' );
        $attr['align'] = $attr['align'] ? "align".esc_attr( $attr['align'] ) : '';
        $attr['size'] = "size-".esc_attr( $attr['size'] );

        // Allow filtering of the default attributes
        $attributes  = apply_filters( 'wp_default_img_attr', $attr );

        // Build class attribute, considering that maybe some attribute was unset via the filter
        $classes  = ' class="';
        $classes .= 'wp-img-default'.esc_attr( $attr['classes'] ? ' '.$attr['classes'] : '' );
        $classes .= $attr['align'] ? ' '.esc_attr( $attr['align'] ) : '';
        $classes .= $attr['size'] ? ' '.esc_attr( $attr['size'] ).'" ' : '" ';

        $url        = trim( $attr['url'] );
        $image      = "<img src='{$url}'{$hwstring}{$classes}{$attr['alt']}{$attr['title']} />";

        // Allow filtering of output
        $image      = apply_filters( 'wp_default_img', $image );

        if ( ! $attr['echo'] )
            return $image;

        return print $image;
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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