Skip to content

Functions and Template Tags

Chris Scott edited this page Mar 27, 2017 · 2 revisions

Other Useful Functions

For advanced themes and usage, the following functions may prove useful.

get_post_thumbnail_id()

Function signature:

MultiPostThumbnails::get_post_thumbnail_id(
    $post_type,
    $id,
    $post_id
)

Get the WordPress post ID of the thumbnail attachment.

  • $post_type required: The post type of the post the thumbnail is set on.
  • $id required: The id used to register the thumbnail.
  • $post_id required: The post ID of the post the thumbnail is set on.

get_post_thumbnail_url()

Function signature:

MultiPostThumbnails::get_post_thumbnail_url(
    $post_type,
    $id,
    $post_id = 0,
    $size = null
)

Get the URL of the thumbnail.

  • $post_type required: The post type of the post the thumbnail is set on.
  • $id required: The id used to register the thumbnail.
  • $post_id optional: The post ID of the post the thumbnail is set on. Defaults to the post ID of the currently queried object.
  • $size optional: The size to use. Defaults to the original size.

the_post_thumbnail()

Function signature:

MultiPostThumbnails::the_post_thumbnail(
    $post_type,
    $thumb_id,
    $post_id = null,
    $size = 'post-thumbnail',
    $attr = '' ,
    $link_to_original = false
)

Echo the post thumbnail.

  • $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?

get_the_post_thumbnail()

Function signature:

MultiPostThumbnails::get_the_post_thumbnail(
    $post_type,
    $thumb_id,
    $post_id = null,
    $size = 'post-thumbnail',
    $attr = '' ,
    $link_to_original = false
)

Get the thumbnail but don't echo it. This uses the same arguments as MultiPostThumbnails::the_post_thumbnail() as detailed above.

has_post_thumbnail()

Function signature:

MultiPostThumbnails::has_post_thumbnail(
    $post_type,
    $id,
    $post_id = null
)

Does the post have the specified thumbnail?

  • $post_type required: The post type of the post the thumbnail is set on.
  • $id required: The id used to register the thumbnail.
  • $post_id optional: The post ID of the post the thumbnail is set on. Defaults to the post ID of the currently queried object.