Fire Studios

Archive

Contact

Categories

  • Coding
    • CSS
    • HTML/Javascript
    • PHP
  • Design
  • Freebies
  • General
  • Internet
  • Interviews
  • Plugins
  • Podcasts
  • Reviews
  • Screencasts
  • Tutorial
  • Weekday Tips
  • Wordpress

Podcast

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;
	}
};

Reader's Thoughts

  • steve

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

  • Fire G

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

  • Adam

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

  • shureiman

    A big site here :) Never seen the same.

Your Thoughts?

 

This is that small text that tells you not to steal our stuff, but we don't really care too much.