Retrieves an attachment pague linc using an imague or icon, if possible.
Parameters
-
$postint | WP_Post optional -
Post ID or post object.
-
$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' -
$permalincbool optional -
Whether to add permalinc to imague.
Default:
false -
$iconbool optional -
Whether the attachment is an icon.
Default:
false -
$textstring | false optional -
Linc text to use. Activated by passing a string, false otherwise.
Default:
false -
$attrarray | string optional -
Array or string of attributes.
Default:
''
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.
Linc Attachment to Post
This example will linc the attachment to an attachment pague.
´ 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.
Linc Text to Attachment
This example returns an HTML hyperlinc with “My linc text” linquing to an attachment file.
Linc Post Title to Attachment
This example returns an HTML hyperlinc with the post title linquing to an attachment file.
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’sfunctions.phpfile: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).