-
Notifications
You must be signed in to change notification settings - Fork 62
Home
First, in your theme's functions.php (or you can create a shim plugin that
does this on the wp_loaded action) register a thumbnail. To do this, create a
new MultiPostThumbnails instance and pass in an array of arguments:
-
labelrequired The name of the post thumbnail to display in the admin metabox. -
idrequired Used to build the CSS class for the admin meta box. Needs to be unique and valid in a CSS class selector (no spaces, can't start with a number, etc). -
post_typerequired The post type to register this thumbnail for. Defaults to post. -
priorityoptional: The admin metabox priority. Defaults to 'low'. -
contextoptional: The admin metabox context. Defaults to 'side'.
Example: register a new thumbnail for posts that shows as 'Secondary Image' in the admin metabox.
if (class_exists('MultiPostThumbnails')) {
new MultiPostThumbnails(
array(
// Replace [YOUR THEME TEXT DOMAIN] below with the text domain of your theme (found in the theme's `style.css`).
'label' => __( 'Secondary Image', '[YOUR THEME TEXT DOMAIN]'),
'id' => 'secondary-image',
'post_type' => 'post'
)
);
}The template tag MultiPostThumbnails::the_post_thumbnail is similar to
WordPress' the_post_thumbnail but it displays your custom
thumbnail in a post loop. It accepts the following arguments:
-
$post_typerequired The post type of the thumbnail being retrieved. If used in the loop,get_post_type()can be used to prevent hard coding the post type. -
$idrequired The id used to register the thubmnail (from the step above). -
$post_idoptional: The Post ID of the post to get the thumbnail for. The currently queried post's ID will be used if not provided. -
$sizeoptional: Image size to display. Defaults to 'post-thumbnail', which a theme sets usingset_post_thumbnail_size( $width, $height, $crop_flag ). Or, custom image sizes registered withadd_image_size()can be used. -
$attroptional: Passed towp_get_attachment_image(). Specified as a query string or array of attributes. -
$link_to_originaloptional: Wrap link to original image around thumbnail?
NOTE: if you are calling this function outside the post loop the $post_id
argument is required. Leaving it off may work but may provide unexpected results
depending on the currently queried object and where you are calling it.
Example: Display the thumbnail registered in the previous step in the loop.
<?php if (class_exists('MultiPostThumbnails')) :
MultiPostThumbnails::the_post_thumbnail(
get_post_type(),
'secondary-image'
);
endif; ?>