wp_get_attachment_thumb_url() WordPress Function

The wp_get_attachment_thumb_url() function retrieves the URL for an image attachment's thumbnail. The function can be used with both image and non-image attachments, but it will only return a thumbnail URL for image attachments. If no thumbnail exists for the attachment, then the function will return the attachment's URL.

wp_get_attachment_thumb_url( int $post_id ) #

Retrieve URL for an attachment thumbnail.


Parameters

$post_id

(int)(Optional) Attachment ID. Default is the ID of the global $post.


Top ↑

Return

(string|false) Thumbnail URL on success, false on failure.


Top ↑

Source

File: wp-includes/post.php

function wp_get_attachment_thumb_url( $post_id = 0 ) {
	$post_id = (int) $post_id;
	$post    = get_post( $post_id );

	if ( ! $post ) {
		return false;
	}

	$url = wp_get_attachment_url( $post->ID );
	if ( ! $url ) {
		return false;
	}

	$sized = image_downsize( $post_id, 'thumbnail' );
	if ( $sized ) {
		return $sized[0];
	}

	$thumb = wp_get_attachment_thumb_file( $post->ID );
	if ( ! $thumb ) {
		return false;
	}

	$url = str_replace( wp_basename( $url ), wp_basename( $thumb ), $url );

	/**
	 * Filters the attachment thumbnail URL.
	 *
	 * @since 2.1.0
	 *
	 * @param string $url     URL for the attachment thumbnail.
	 * @param int    $post_id Attachment ID.
	 */
	return apply_filters( 'wp_get_attachment_thumb_url', $url, $post->ID );
}


Top ↑

Changelog

Changelog
VersionDescription
2.1.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More