Forums

Choosing Default Thumbnail (3 posts)

  1. nathanmcginty
    Member
    Posted 9 months ago #

    Hey guys,

    Switching over to Wordpress from Drupal and I like what I see so far.

    I'm using the wp-magazine theme. Got a number of columns with excerpts sorted by category that I modified the Loop on and I'm pretty happy.

    One thing I'm trying to do - I'd like it so that if images aren't used in a post, WP will pull up a default thumbnail to use for the post on the front page. It would be even better if I could change the thumbnail so that for one category, it pulls up one thumbnail, for another category another, etc.

    I'm fairly comfortable with messing about with the PHP code, but I can't quite seem to figure out how to pull this off.

    Any advice would be appreciated.

  2. CallieJo
    Member
    Posted 7 months ago #

    I'm also looking for a way to have a default thumbnail show up without manually adding the same one to each and every entry that doesn't have images uploaded to them.

  3. mfields
    Member
    Posted 7 months ago #

    I answered a similar question here. The user seems to be using one of those "magazine themes" which lets you supply an image for the homepage via custom fields.

    There is an easier way of doing this though. Please see this thread.

    As far as a different default image for each category, something like this would probably work (inside the loop):

    <?php
    $custom_field_key = 'thumbnail_url';
    $default_url = 'http://example.com/default-image.jpg';
    $category_thumb_urls = array(
    	3 => 'http://example.com/image-1.jpg',
    	4 => 'http://example.com/image-2.jpg',
    	5 => 'http://example.com/image-3.jpg'
    	);
    
    $url = get_post_meta( $post->ID, $custom_field_key, true );
    
    if( !empty( $url ) &amp;&amp; ( list( $w, $h ) = getimagesize( $url ) ) ) {
    	$thumbnail = $url;
    }
    elseif( in_category( array_keys( $category_thumb_urls ) ) ) {
    	foreach( $category_thumb_urls as $k => $v ) {
    		if( in_category( $k ) ) {
    			if( list( $w, $h ) = getimagesize( $v ) )
    				$thumbnail = $v;
    		}
    	}
    }
    elseif( list( $w, $h ) = getimagesize( $default_url ) )
    	$thumbnail = $default_url;
    else
    	$thumbnail = false;
    
    if ( $thumbnail )
    	print '<img src="' . $thumbnail . '" width="' . $w . '" height="' . $h . '" alt="" />';
    ?>

    you'll want to change the values for the first 3 variables to match how your blog is set up. The array consists of category id for keys and thumbnail path as values. You shouldn't need to edit anything below this.

    Hope this helps,
    -Mike

Reply

You must log in to post.

About this Topic