• Skip to primary navigation
  • Skip to main content
  • Skip to footer
  • Home
  • About
  • My Blogs
    • Photoblog
    • Knots
    • WP.com tips
kristarella.com

kristarella.com

Happiness Engineer at Automattic, lover of knitting, crochet, sci-fi and more

  • Presentations
  • Plugins
    • Exifography
  • Contact
You are here: Home / WordPress / Themes / Thesis / Handy WordPress thumbnail function

Handy WordPress thumbnail function

10 September 2012 by kristarella

Contents
  1. New Image Size
  2. Using the function
    1. Parameters
    2. Call custom_thumb()
    3. Prefer the feature image over Thesis thumbnail
    4. Adapting custom_thumb() for other themes

This is one of my favourite code snippets at the moment.

It enables WordPress post thumbnails (feature image), adds a new image size (thumbnail, medium and large are included by default in WordPress and their sizes can be set in the WordPress media settings), then it creates a function that when called displays the Thesis Post Image thumbnail, or the feature image, or the first image attached to the post.

In Thesis this code goes in custom_functions.php.

add_theme_support('post-thumbnails');
add_image_size('feature_image','215','110',true);
function custom_thumb($classes='thumb',$size='thumbnail',$thesis_thumb=false) {
	global $post;
	
	$post_image = thesis_post_image_info('thumb');
	$post_thumb = get_the_post_thumbnail($post->ID,$size,array('class'=> $classes));
	$images =& get_children(array(
		'post_parent' => $post->ID,
		'post_type' => 'attachment',
		'numberposts' => 1,
		'post_mime_type' => 'image',
		'orderby' => 'ID',
		'order' => 'ASC')
	);

	if ($post_image['output'] && $thesis_thumb == true)
		echo '<a href="'.get_permalink().'"><img class="'.$classes.'" src="' . $post_image['url'] . '" alt="Thumbnail image ' . get_the_title() . '" /></a>';
	elseif ($post_image['output'] && $thesis_thumb == false)
		return false;
	elseif ($post_thumb)
		echo '<a href="'.get_permalink().'">'.$post_thumb.'</a>';
	elseif ($images) {
		foreach ($images as $image)
			$img = wp_get_attachment_image_src($image->ID, $size, false);
				
		echo '<a href="'.get_permalink().'"><img class="'.$classes.'" src="' . $img[0] . '" alt="Thumbnail image ' . get_the_title() . '" /></a>';
	}
	else
		return false;
}

New Image Size

In the second line, add_image_size('feature_image','215','110',true); the first argument is the name of the image that you would normally use in the_post_thumbnail() and get_the_post_thumbnail() to use that image size. The second argument is the width, third height, and fourth is whether the image is hard cropped: true means the the image will be scaled and then cropped to exactly those dimensions, false means the image will be proportionally scaled to fit within those dimensions.

Using the function

Parameters

The new function custom_thumb() accepts 3 optional parameters. The first are any classes that you want to apply to your thumbnail image (such as alignleft to float the image left). The second argument is the image size, which defaults to thumbnail, but you can use medium, large, full or any additional sizes you have added. The third parameter is whether you want to display the Thesis Post Image thumbnail or not; the default is false (do not display Post Image thumbnail), true will display the Post Image thumb. If you call the function in a Thesis teaser you will want to leave the Post Image thumbnail off (false) because it is automatically displayed by Thesis, but if it’s in a full post, post excerpt or custom loop you can turn the $thesis_thumb option on.

Call custom_thumb()

To use custom_thumb() in Thesis with the default options you can insert the function directly via a hook. E.g., add_action('thesis_hook_before_teaser','custom_thumb');
For the full list of Thesis hooks please visit the user manual.

To use the function with parameters other than the defaults use it within another function. E.g.,

function add_my_custom_thumb() {
	custom_thumb('thumb aligncenter','feature_image',true);
}
add_action('thesis_hook_before_post','add_my_custom_thumb');

Prefer the feature image over Thesis thumbnail

To use the WordPress Post Thumbnail when one is assigned preferentially over the Thesis Post Image thumbnail change this chunk of the function

	if ($post_image['output'] && $thesis_thumb == true)
		echo '<a href="'.get_permalink().'"><img class="'.$classes.'" src="' . $post_image['url'] . '" alt="Thumbnail image ' . get_the_title() . '" /></a>';
	elseif ($post_image['output'] && $thesis_thumb == false)
		return false;
	elseif ($post_thumb)
		echo '<a href="'.get_permalink().'">'.$post_thumb.'</a>';

to

	if ($post_thumb)
		echo '<a href="'.get_permalink().'">'.$post_thumb.'</a>';
	elseif ($post_image['output'] && $thesis_thumb == true)
		echo '<a href="'.get_permalink().'"><img class="'.$classes.'" src="' . $post_image['url'] . '" alt="Thumbnail image ' . get_the_title() . '" /></a>';
	elseif ($post_image['output'] && $thesis_thumb == false)
		return false;

Adapting custom_thumb() for other themes

If you want to use the custom_thumb() function with any other theme, paste the code in your theme’s functions.php and remove $post_image = thesis_post_image_info('thumb'); and

	if ($post_image['output'] && $thesis_thumb == true)
		echo '<a href="'.get_permalink().'"><img class="'.$classes.'" src="' . $post_image['url'] . '" alt="Thumbnail image ' . get_the_title() . '" /></a>';
	elseif ($post_image['output'] && $thesis_thumb == false)
		return false;

and change elseif ($post_thumb) to if ($post_thumb).

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X
  • Click to share on Pocket (Opens in new window) Pocket

Like this:

Like Loading...

Related

Filed Under: Thesis, Tutorials, WordPress Tagged With: PHP

Reader Interactions

Comments

  1. Mike Macias | The Mobile Fanatics says

    12 September 2012 at 13:30

    Thanks for the tutorial. I’m going to give it a shot asap.

    So this functionality is essentially the same as this: http://thesistut.com/store/wor.....ge-thesis/

    Correct?

  2. kristarella says

    12 September 2012 at 14:23

    Mike — Not exactly… it doesn’t auto-populate the Thesis Post Image, or provide any admin options, or add an image thumb in the admin post list. In essence it adds a similar level of convenience, but with this you have to control it via PHP whereas the plugin provides multiple admin options.

  3. Craig Grella says

    20 September 2012 at 07:54

    I struggled through hacking my own code for this very same functionality (plus I added some that adds a stock logo if no image or thumb is present).
    But my code was not nearly as neat as yours and it took me hours to figure out.
    Thanks for this.

    As a follow up, how would this be modified to use this with custom post types?

  4. kristarella says

    20 September 2012 at 09:08

    Craig — You shouldn’t need to modify it much or at all for custom post types, you can just use custom_thumb(); in your custom post type loop, or to use a hook you’d use a conditonal tag:

    function add_my_custom_thumb() {
    	if ('book' == get_post_type())
    		custom_thumb();
    }
    add_action('thesis_hook_before_post','add_my_custom_thumb');

    Where “book” is the post type.

A triptych of baubles. These are very satisfying t A triptych of baubles. These are very satisfying to paint ❤️💚💙

#watercolor #watercolour #christmas
I have really enjoyed this Christmassy painting! S I have really enjoyed this Christmassy painting! So glad my friend asked me to make some cards for our church helpers, because it set me on a roll!
#watercolor #watercolour #christmas #christmascards #watercolorcards
Had a great Saturday at the GKR Karate State Title Had a great Saturday at the GKR Karate State Titles! I was a sub for @jamesxuereb.me in the Blue Flame Dragons thanks to his sprained ankle; we won first round and came fourth overall!
I did a bunch of judging and officiating, which was really good.
I didn’t place in my individual events, but had a very fun final round of kumite (sparring).

#gkrkarate #karate
More stamping tonight. Even better than last night More stamping tonight. Even better than last night’s!
Did some stamping this evening. Love it! I wish I’d done some pages in other ink colours before I dismantled the stamps 😂
Had an appointment in the city, so I got to visit Had an appointment in the city, so I got to visit the @legocertifiedstores_anz Wicked display!
#wicked #lego #afol #sydney
A little book I made from Bunnings paint sample ca A little book I made from Bunnings paint sample cards. It’s going to be for mini paintings and collages. Sometimes it’s nice to start with a colour rather than a blank white page!
A little while ago I did some swatching of Daniel A little while ago I did some swatching of Daniel Smith and Schminke Horodam watercolours. So soothing! I love some of the granulating colours!
#watercolours
Follow on Instagram

Footer

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Top Posts & Pages

  • Home
    Home
  • Sistaco Nail Powder Review
    Sistaco Nail Powder Review
  • Exifography
    Exifography
  • Bible reading printouts
    Bible reading printouts

Follow Me On…

  • Instagram
  • X
  • GitHub
  • LinkedIn
  • YouTube

Categories

Copyright © 2025 · Kristen Symonds · kristarella.com

%d