Skip to content
Chris Scott edited this page Jul 11, 2018 · 5 revisions

Quick Start

Register a new Thumbnail

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:

  • label required The name of the post thumbnail to display in the admin metabox.
  • id required 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_type required The post type to register this thumbnail for. Defaults to post.
  • priority optional: The admin metabox priority. Defaults to 'low'.
  • context optional: 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'
        )
    );
}

Display the Thumbnail

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_type required 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.
  • $id required The id used to register the thubmnail (from the step above).
  • $post_id optional: The Post ID of the post to get the thumbnail for. The currently queried post's ID will be used if not provided.
  • $size optional: Image size to display. Defaults to 'post-thumbnail', which a theme sets using set_post_thumbnail_size( $width, $height, $crop_flag ). Or, custom image sizes registered with add_image_size() can be used.
  • $attr optional: Passed to wp_get_attachment_image(). Specified as a query string or array of attributes.
  • $link_to_original optional: 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; ?>