Thord Daniel Hedengren - Smashing WordPress_ Beyond the Blog-Wiley (2014)

(avery) #1

CHAPTER 6 • Advanced Theme Usage 145


// Add support for featured images
add_theme_support( 'post-thumbnails' );


}


Naturally, this function could contain more than just one feature like this. Again, if you need
to, revisit Chapter 4 and the functions.php file from Simple Blog.


It is preferred to use functions and hooks like this in functions.php rather than just drop the
code there. You could, but it is better to know for sure when the code will initiate using hooks.


The following subsections will omit the hook and function to save space. Just add the features
to the preceding function, and you’ll be fine.


FEATURED IMAGES


Featured images, previously known as post thumbnails, are a theme feature that lets the user
select an image representing the post using a simple dialog box on the Edit Post page in the
admin interface. Enabling it is easy enough; just use add_theme_support() and pass the
parameter post-thumbnails to it, in functions.php:


add_theme_support( 'post-thumbnails' );


This will enable the Featured Image box for posts in the admin interface. It is possible to add
support for featured images on custom post types as well, for example. Figure 6-4 shows the
Featured Image box, which lets the user pick an image as a featured image for a post.


Figure 6-4: The Featured Image box in the bottom right.


You might want to add an additional image size for your featured images. With addimage
size(), you can register new image sizes, which will be created with every upload much like
the thumbnail and medium image sizes, for example. The following code, which should be
used with the after_setup_theme hook, will add an image size called top-feature,
scaled to 500 x 225 pixels. If you want it to just scale by width and not height, change 225 to
9999 instead.


add_image_size( 'top-feature', 500, 225 );

Free download pdf