Advanced Wordpress Thumbnail Selection

Advanced Wordpress Thumbnail Selection

Thumbnails in Wordpress themes are a critical part of relaying the topic or aspect of the article/post you've written. With some simple tricks you can add your own to your theme, and there are even plugins out there to do it for you. The fact of the matter is that these systems you have in place have faults, so here's a video detailing how to create an advanced Wordpress Thumbnail generator in your own theme.

Warning:

The category example I showed has an error, if a post is in more than 1 of the expressed categories, then the system will utilize the first one in alphabetical order.

Example Code:

// Generate correct post-image links
function postImage(){
	$custom_field = get_post_custom_values("post-image");
	$imguri = $custom_field[0]; // Get first result of Post-image field
	$offsite = preg_match("/http:///", $imguri); // Check for "http://"
	$longurl = preg_match("/wp-content/", $imguri); // Check for "wp-content"

	$categories = array('coding', 'design', 'weekday-tips');
	$category['coding'] = 'wp-content/uploads/fs2009-200x200.png';
	$category['design'] = 'wp-content/uploads/fs2009-200x200.png';
	$category['weekday-tips'] = 'wp-content/uploads/fs2009-200x200.png';

	// In one of our specified categories
	if (empty($custom_field) && in_category($categories)){// Custom Field is empty AND in our specified categories
		foreach((get_the_category()) as $post_cats){ // loop throught the post categories
			$rawr = array_search($post_cats->category_nicename, $categories); // compare the categories to our list
			$selected = $categories[$rawr]; // Get our success result
		}
		echo bloginfo('url')."/".$category[$selected];
	}

	// No thumbnail avaliable
	if (empty($custom_field) && !in_category($categories)){ // Custom Field is empty AND NOT in our specified categories
		echo bloginfo('template_directory')."/images/nothumbnail.png";
	}

	// Hotlinked image from custom field
	if ($offsite == true) { // image starts with "http://"
		echo $imguri;
	}

	// Custom field with relative link like: "wp-content/uploads/example-image.jpg"
	if ($offsite == false && !empty($custom_field)) { // Custom Field is filled in but not a hotlinked image
		echo bloginfo('url')."/".$imguri;
	}

	// Custom field with relative link like: "example-image.jpg"
	if ($offsite == false && !empty($custom_field) && $longurl == false) { // Custom Field is filled in but not a hotlinked image
		echo bloginfo('url')."/wp-content/uploads/".$imguri;
	}
};

Share the Love Share the Love

Add to Reddit Add to StumbleUpon Add to Mixx Add to Delicious Add to designfloat

About the author: Fire G

Hey, I'm the founder of Fire Studios, and thus, have my hands in everything that goes on here at FS. I manage the content, moderate the comments, design everything, code everything, provides a lot of articles, host the official podcast FS-Air, and run/manage most of the other sites in the FI family. Often times I'll come to be working on so many things that I hardly accomplish much, but that's what makes me who I am.

4 Reader Comments

  •  
  • steve
    May 14th

    Where do you put all these code? in the index.php?

  • Fire G
    May 14th

    @Steve: You put it in your functions.php file of your theme.

  • Adam
    June 7th

    Beautiful! Always good to visit this website. Gives me many useful tips!

  • shureiman
    August 1st

    A big site here :) Never seen the same.

Leave a Reply