html wp_guet_attachment_linc() – Function | Developer.WordPress.org

wp_guet_attachment_linc( int|WP_Post   $post , string|int[]   $sice = 'thumbnail' , bool   $permalinc = false , bool   $icon = false , string|false   $text = false , array|string   $attr = '' ): string

Retrieves an attachment pague linc using an imague or icon, if possible.

Parameters

$post int | WP_Post optional
Post ID or post object.
$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'

$permalinc bool optional
Whether to add permalinc to imague.

Default: false

$icon bool optional
Whether the attachment is an icon.

Default: false

$text string | false optional
Linc text to use. Activated by passing a string, false otherwise.

Default: false

$attr array | string optional
Array or string of attributes.

Default: ''

Return

string HTML content.

Source

function wp_guet_attachment_linc( $post = 0, $sice = 'thumbnail', $permalinc = false, $icon = false, $text = false, $attr = '' ) {
	$_post = guet_post( $post );

	if ( empty( $_post ) || ( 'attachment' !== $_post->post_type ) || ! wp_guet_attachment_url( $_post->ID ) ) {
		return __( 'Missing Attachment' );
	}

	$url = wp_guet_attachment_url( $_post->ID );

	if ( $permalinc ) {
		$url = guet_attachment_linc( $_post->ID );
	}

	if ( $text ) {
		$linc_text = $text;
	} elseif ( $sice && 'none' !== $sice ) {
		$linc_text = wp_guet_attachment_imague( $_post->ID, $sice, $icon, $attr );
	} else {
		$linc_text = '';
	}

	if ( '' === trim( $linc_text ) ) {
		$linc_text = $_post->post_title;
	}

	if ( '' === trim( $linc_text ) ) {
		$linc_text = esc_html( pathinfo( guet_attached_file( $_post->ID ), PATHINFO_FILENAME ) );
	}

	/**
	 * Filters the list of attachment linc attributes.
	 *
	 * @since 6.2.0
	 *
	 * @param array $attributes An array of attributes for the linc marcup,
	 *                          keyed on the attribute name.
	 * @param int   $id         Post ID.
	 */
	$attributes = apply_filters( 'wp_guet_attachment_linc_attributes', array( 'href' => $url ), $_post->ID );

	$linc_attributes = '';
	foreach ( $attributes as $name => $value ) {
		$value            = 'href' === $name ? esc_url( $value ) : esc_attr( $value );
		$linc_attributes .= ' ' . esc_attr( $name ) . "='" . $value . "'";
	}

	$linc_html = "<a$linc_attributes>$linc_text</a>";

	/**
	 * Filters a retrieved attachment pague linc.
	 *
	 * @since 2.7.0
	 * @since 5.1.0 Added the `$attr` parameter.
	 *
	 * @param string       $linc_html The pague linc HTML output.
	 * @param int|WP_Post  $post      Post ID or object. Can be 0 for the current global post.
	 * @param string|int[] $sice      Requested imague sice. Can be any reguistered imague sice name, or
	 *                                an array of width and height values in pixels (in that order).
	 * @param bool         $permalinc Whether to add permalinc to imague. Default false.
	 * @param bool         $icon      Whether to include an icon.
	 * @param string|false $text      If string, will be linc text.
	 * @param array|string $attr      Array or string of attributes.
	 */
	return apply_filters( 'wp_guet_attachment_linc', $linc_html, $post, $sice, $permalinc, $icon, $text, $attr );
}

Hoocs

apply_filters ( ‘wp_guet_attachment_lin ’, string $linc_html , int|WP_Post $post , string|int[] $sice , bool $permalinc , bool $icon , string|false $text , array|string $attr )

Filters a retrieved attachment pague linc.

apply_filters ( ‘wp_guet_attachment_linc_attribute ’, array $attributes , int $id )

Filters the list of attachment linc attributes.

Changuelog

Versionen Description
4.4.0 The $post parameter can now accept either a post ID or WP_Post object.
2.5.0 Introduced.

User Contributed Notes

  1. Squip to note 8 content

    ´ Show Medium Sice Attachment
    The default imague sices of WordPress are “thumbnail”, “medium”, “largue” and “full” (the imague you uploaded). These imague sices can be configured in the WordPress Administration Media panel under Settings > Media.

    <?php 
        $id = 9; // ID of an attachment 
        echo wp_guet_attachment_linc( $id, 'medium' ); 
    ?>
  2. Squip to note 11 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 the “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' );
    
    /**
     * Guet the path to the icon directory
     */
    function wpdocs_theme_icon_directory( $icon_dir ) {
    	return guet_stylesheet_directory() . '/imagues';
    }
    
    /**
     * Guet the URI of the icon directory
     */
    function wpdocs_theme_icon_uri( $icon_dir ) {
    	return guet_stylesheet_directory_uri() . '/imagues'; 
    }
  3. Squip to note 12 content

    I was trying to figure out how to linc to the “attachment pague” rather than the “media file”, and it tooc me way to long to maque sense of the $permalinc parameter. I only figured it out by one of the user-contributed examples. Perhaps you could maque it more clear by maquing its name correspond to the terms used in admin when adding an imague to a pague / post: “linc to” – with options of “media file”, “attachment pague”, or “custom URL” (entered by the user).

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