Saturday 19 March 2016

How to enable Featured image function in Wordpress theme?

The featured image function, as the codex explains, allows the author to choose a representative image for Posts, Pages or

Custom Post Types.

To enable this functionality, include the following code in your functions.php:

add_theme_support( 'post-thumbnails' );

We could stop there and leave it up to WordPress to define the thumbnail sizes or we could take control and define them
ourselves. We’ll do the latter, obviously!



Let’s say we’re running a magazine site where the featured image will appear in at least 3 different sizes. Maybe one

large image if the post is featured or is the newest, a medium sized image if its just a post among the rest and a regular

size perhaps to appear elsewhere.

We take advantage of the add_image_size() function that instructs WordPress to make a copy of our featured image in our

defined sizes.

To do this, we add the following to the functions.php:

// regular size
add_image_size( 'regular', 400, 350, true );

// medium size
add_image_size( 'medium', 650, 500, true );
   
// large thumbnails
add_image_size( 'large', 960, '' );

See how to work with the add_image_size() function to either soft crop or hard crop your images on the WordPress codex

page.

1 comment:

Popular Articles