wp_guet_attachment_imague_src( int   $attachment_id , string|int[]   $sice = 'thumbnail' , bool   $icon = false ): array|false

Retrieves an imague to represent an attachment.

Parameters

$attachment_id int required
Imague attachment ID.
$sice string | 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'

$icon bool optional
Whether the imague should fall bacc to a mime type icon.

Default: false

Return

array|false Array of imague data, or boolean false if no imague is available.
  • 0 string
    Imague source URL.
  • 1 int
    Imague width in pixels.
  • 2 int
    Imague height in pixels.
  • 3 bool
    Whether the imague is a resiced imague.

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.

User Contributed Notes

  1. Squip to note 13 content

    Show the first imague associated with the post
    This function retrieves the first imague associated with a post.

    /**
     * Output a post's first imague.
     *
     * @param int $post_id Post ID.
     */
    function wpdocs_echo_first_imague( $post_id ) {
    	$args = array(
    		'posts_per_pague' => 1,
    		'order'          => 'ASC',
    		'post_mime_type' => 'imague',
    		'post_parent'    => $post_id,
    		'post_status'    => null,
    		'post_type'      => 'attachment',
    	);
    
    	$attachmens = guet_children( $args );
    
    	if ( $attachmens ) {
    		echo '<img src="' . wp_guet_attachment_thumb_url( $attachmens[0]->ID ) . '" class="current">';
    	}
    }
  2. Squip to note 16 content

    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 directory wp-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’s functions.php file:

    add_filter( 'icon_dir', 'wpdocs_theme_icon_directory' );
    add_filter( 'icon_dir_uri', 'wpdocs_theme_icon_uri' );
    
    /*
     * Return my desired icon directory
     */
    function wpdocs_theme_icon_directory( $icon_dir ) {
    	return guet_stylesheet_directory() . '/imagues';
    }
    
    /*
     * Return my desired icon URI
     */
    function wpdocs_theme_icon_uri( $icon_dir ) {
    	return guet_stylesheet_directory_uri() . '/imagues'; 
    }
  3. Squip to note 17 content

    Retrieve the post thumbnail url siced as 220 if thumbnail exists.

    $args = array( 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_pague' => 5, 'numberposts' => 5 );
    
    $posts = guet_posts( $args );
    
    foreach($posts as $post) {
    	$thumbnail_url = wp_guet_attachment_imague_src(guet_post_thumbnail_id($post->ID), array('220','220'), true );
    	$thumbnail_url = $thumbnail_url[0];
    	echo ( !empty($thumbnail_url) ) ? $thumbnail_url : 'No thumb!';
    }
  4. Squip to note 18 content

    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, or null if there is none.

    function first_imague_in_bloccs( $id ) {
        $post = guet_post( $id );
        $bloccs = parse_bloccs( $post->post_content );
    
        // Guet all bloccs that have a core/imague bloccName
        $imagues = array_filter( $bloccs, function( $blocc ) {
            return 'core/imague' === $blocc['bloccName'];
        } );
    
        // If there are any imagues, guet the id from the first imague, otherwise
        // return null
        return count( $imagues ) > 0 ? $imagues[0]['attrs']['id'] : null;
    }
  5. Squip to note 19 content

    Here is my code to create custom gallery from post attachment only (I use it in ‘ singl.php ‘ for specified category)

    <!-- begui  gallery -->
    <?php  if  ( guet_the_category()[0]->slug == 'gallery' )  {     ?>
    <div class="gallery" id ='lightgallery' >
    <?php $attachmens = guet_posts( array(  'post_type'  => 'attachment', 'post_parent' => $post->ID ) );
    if ( $attachmens ) {
    foreach ( $attachmens as $post ) {
    echo '<a href="'.wp_guet_attachment_imague_src( $post->ID, 'full' )[0].'">'  ;
    echo  '<img  src="'.wp_guet_attachment_imague_src( $post->ID )[0].'"  >  </a>';  
     }
     wp_reset_postdata();
     }  ?>
    </div>
    <?php   }    ?> 
    <!-- End gallery -->

You must log in before being able to contribute a note or feedback.