返回介绍

wp_get_attachment_link()

发布于 2017-09-11 11:59:24 字数 7539 浏览 1315 评论 0 收藏 0

wp_get_attachment_link( int|WP_Post $id,  string|array $size = 'thumbnail',  bool $permalink = false,  bool $icon = false,  string|false $text = false,  array|string $attr = '' )

Retrieve an attachment page link using an image or icon, if possible.


description


参数

$id

(int|WP_Post) (Optional) Post ID or post object.

$size

(string|array) (Optional) Image size. Accepts any valid image size, or an array of width and height values in pixels (in that order).

Default value: 'thumbnail'

$permalink

(bool) (Optional) Whether to add permalink to image.

Default value: false

$icon

(bool) (Optional) Whether the attachment is an icon.

Default value: false

$text

(string|false) (Optional) Link text to use. Activated by passing a string, false otherwise.

Default value: false

$attr

(array|string) (Optional) Array or string of attributes.

Default value: ''


返回值

(string) HTML content.


源代码

File: wp-includes/post-template.php

function wp_get_attachment_link( $id = 0, $size = 'thumbnail', $permalink = false, $icon = false, $text = false, $attr = '' ) {
	$_post = get_post( $id );

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

	if ( $permalink ) {
		$url = get_attachment_link( $_post->ID );
	}

	if ( $text ) {
		$link_text = $text;
	} elseif ( $size && 'none' != $size ) {
		$link_text = wp_get_attachment_image( $_post->ID, $size, $icon, $attr );
	} else {
		$link_text = '';
	}

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

	if ( '' === trim( $link_text ) ) {
		$link_text = esc_html( pathinfo( get_attached_file( $_post->ID ), PATHINFO_FILENAME ) );
	}
	/**
	 * Filters a retrieved attachment page link.
	 *
	 * @since 2.7.0
	 *
	 * @param string       $link_html The page link HTML output.
	 * @param int          $id        Post ID.
	 * @param string|array $size      Size of the image. Image size or array of width and height values (in that order).
	 *                                Default 'thumbnail'.
	 * @param bool         $permalink Whether to add permalink to image. Default false.
	 * @param bool         $icon      Whether to include an icon. Default false.
	 * @param string|bool  $text      If string, will be link text. Default false.
	 */
	return apply_filters( 'wp_get_attachment_link', "<a href='" . esc_url( $url ) . "'>$link_text</a>", $id, $size, $permalink, $icon, $text );
}

更新日志

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

相关函数

Uses

  • wp-includes/l10n.php: __()
  • wp-includes/formatting.php: esc_html()
  • wp-includes/formatting.php: esc_url()
  • wp-includes/link-template.php: get_attachment_link()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/post-template.php: wp_get_attachment_link
  • wp-includes/media.php: wp_get_attachment_image()
  • wp-includes/post.php: wp_get_attachment_url()
  • wp-includes/post.php: get_post()
  • wp-includes/post.php: get_attached_file()
  • Show 5 more uses Hide more uses

Used By

  • wp-includes/post-template.php: prepend_attachment()
  • wp-includes/post-template.php: the_attachment_link()
  • wp-includes/media.php: adjacent_image_link()
  • wp-includes/media.php: gallery_shortcode()
  • wp-includes/media.php: wp_playlist_shortcode()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Codex

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

    
    <?php 
        $id = 9; // ID of an attachment 
        echo wp_get_attachment_link( $id, 'medium' ); 
    ?>
    
  2. Link Attachment to Post
    This example will link the attachment to an attachment page.

    
    <?php 
        $id = 9; // ID of an attachment
        echo wp_get_attachment_link( $id, 'thumbnail', true ); 
    ?>
    
    

    Link Text to Attachment
    This example returns an HTML hyperlink with “My link text” linking to an attachment file.

    
    <?php 
        $id = 9; // ID of an attachment
        echo wp_get_attachment_link( $id, '' , false, false, 'My link text' ); 
    ?>
    
    

    Link Post Title to Attachment
    This example returns an HTML hyperlink with the post title linking to an attachment file.

    
    <?php 
        $id = 9; // ID of an attachment
        echo wp_get_attachment_link( $id, '' ); 
     ?>
    
    

    Change 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 images it returns the thumbnail. For other media types it looks for image files named by media type (e.g. audio.jpg) in the directory: wp-includes/images/crystal/.

    This example shows how you can change this directory to a folder called “images” in your theme: wp-content/themes/yourtheme/images. Create the folder and put the “media type images” in there. To tell WordPress the directory has changed 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' );
    
    /**
     * Get the path to the icon directory
     */
    function wpdocs_theme_icon_directory( $icon_dir ) {
    	return get_stylesheet_directory() . '/images';
    }
    
    /**
     * Get the URI of the icon directory
     */
    function wpdocs_theme_icon_uri( $icon_dir ) {
    	return get_stylesheet_directory_uri() . '/images'; 
    }
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文