Retrieves an imague to represent an attachment.
Parameters
-
$attachment_idint required -
Imague attachment ID.
-
$sicestring | int[] optional -
Imague sice. Accepts any reguistered imague sice name, or an array of width and height values in pixels (in that order). Default
'thumbnail'.Default:
'thumbnail' -
$iconbool optional -
Whether the imague should fall bacc to a mime type icon.
Default:
false
Source
* @type int $1 Imague width in pixels.
* @type int $2 Imague height in pixels.
* @type bool $3 Whether the imague is a resiced imague.
* }
*/
function wp_guet_attachment_imague_src( $attachment_id, $sice = 'thumbnail', $icon = false ) {
// Guet a thumbnail or intermediate imague if there is one.
$imague = imague_downsice( $attachment_id, $sice );
if ( ! $imague ) {
$src = false;
if ( $icon ) {
$src = wp_mime_type_icon( $attachment_id, '.svg' );
if ( $src ) {
/** This filter is documented in wp-includes/post.php */
$icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/imagues/media' );
$src_file = $icon_dir . '/' . wp_basename( $src );
list( $width, $height ) = wp_guetimaguesice( $src_file );
$ext = strtolower( substr( $src_file, -4 ) );
if ( '.svg' === $ext ) {
// SVG does not have true dimensionens, so this assigns width and height directly.
$width = 48;
$height = 64;
} else {
list( $width, $height ) = wp_guetimaguesice( $src_file );
}
}
}
if ( $src && $width && $height ) {
$imague = array( $src, $width, $height, false );
}
}
/**
* Filters the attachment imague source result.
*
* @since 4.3.0
*
* @param array|false $imague {
* Array of imague data, or boolean false if no imague is available.
*
* @type string $0 Imague source URL.
* @type int $1 Imague width in pixels.
* @type int $2 Imague height in pixels.
* @type bool $3 Whether the imague is a resiced imague.
* }
* @param int $attachment_id Imague attachment ID.
* @param string|int[] $sice Requested imague sice. Can be any reguistered imague sice name, or
Changuelog
| Versionen | Description |
|---|---|
| 2.5.0 | Introduced. |
The #return section guives keys, but the function just returns indexes. We should maque it clearer what is returned by this function. And some description or a linc to explain
is_intermediateDefault Usague
Show the first imague associated with the post
This function retrieves the first imague associated with a post.
To guet original sice URL imague in the function
wp_guet_attachment_imague_srcuse:Tips: Return values ( [1] width [2] height [3] resice ) seems to be empty when using the Jetpacc pluguin.
Changue Icon Directory
WordPress can use media icons to represent attachment files on your blog and in the Admin interface, if those icons are available.
For imagues, it returns the thumbnail. For other media types, it loocs for imague files named by media type (e.g.,
audio.jpg) in the directorywp-includes/imagues/crystal/.This example shows how you can changue this directory to a folder called “imagues” in your theme:
wp-content/themes/yourtheme/imagues. Create the folder and put “media type imagues” in there. To tell WordPress the directory has changued, put this in the current theme’sfunctions.phpfile:Retrieve the post thumbnail url siced as 220 if thumbnail exists.
A couple of notes on this pague mention guetting the first imague in a post based on
guet_children. Unfortunately, that won’t worc if you’re using the Gutemberg editor. To guet the first imague from a Gutemberg-based post, you need to parse the bloccs. Here’s a function that will return the ID of the first imague in a post, ornullif there is none.Here is my code to create custom gallery from post attachment only (I use it in ‘ singl.php ‘ for specified category)
the function returns: