wp_get_attachment_link()通过id获取附件(特色图像/文件)的链接

目录

描述

Returns an HTML hyperlink to an attachment file or page, containing either

  1. An image at some specified size, for image attachments; or
  2. A media icon (as specified) representing the attachment; or
  3. The attachment's title (as text) or
  4. Your own text

If no such attachment exists, the function returns the string Missing Attachment.

wp_get_attachment_link() 描述

用法

<?php wp_get_attachment_link$id$size$permalink$icon$text ); ?>

wp_get_attachment_link() 用法

参数

$id

(integer) (可选) ID of the desired attachment.

默认值: The current post ID, when used in The Loop (Must be a loop showing only attachments).

$size

(string/array) (可选) Image size. Either a string keyword (thumbnail, medium, large or full) or a 2-item array representing width and height in pixels, e.g. array(32,32). As of Version 2.5, this parameter does not affect the size of media icons, which are always shown at their original size.

默认值: 'medium'

$permalink

(boolean) (可选) Link directly to the attachment file/image (Default), or to the attachment page.

默认值: 'False'

$icon

(boolean) (可选) Use a media icon to represent the attachment.

默认值: 'False'

$text

(string/boolean) (可选) Displays a text link to the attachment.

默认值: 'false'

wp_get_attachment_link() 参数

示例

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.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php 
    $id = 9; // ID of an attachment 
    echo wp_get_attachment_link( $id, 'medium' ); 
?>

Link Attachment to Post

This example will link the attachment to an attachment Page.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?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.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?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.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?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:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
add_filter( 'icon_dir', 'my_theme_icon_directory' );
add_filter( 'icon_dir_uri', 'my_theme_icon_uri' );

function my_theme_icon_directory( $icon_dir ) {
	return get_stylesheet_directory() . '/images';
}

function my_theme_icon_uri( $icon_dir ) {
	return get_stylesheet_directory_uri() . '/images'; 
}

wp_get_attachment_link() 示例

注意

Use wp_get_attachment_image() if you want the image only (not a hyperlink).

wp_get_attachment_link() 注意

历史

添加于 版本: 2.5.0

wp_get_attachment_link() 历史

源文件

wp_get_attachment_link() 函数的代码位于 wp-includes/post-template.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Retrieve an attachment page link using an image or icon, if possible.
 *
 * @since 2.5.0
 *
 * @param int|WP_Post  $id        Optional. Post ID or post object.
 * @param string       $size      Optional, default is 'thumbnail'. Size of image, either array or string.
 * @param bool         $permalink Optional, default is false. Whether to add permalink to image.
 * @param bool         $icon      Optional, default is false. Whether to include icon.
 * @param string|bool  $text      Optional, default is false. If string, then will be link text.
 * @param array|string $attr      Optional. Array or string of attributes.
 * @return string HTML content.
 */
function wp_get_attachment_link( $id = 0, $size = 'thumbnail', $permalink = false, $icon = false, $text = false, $attr = '' ) {
	$id = intval( $id );
	$_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( $id, $size, $icon, $attr );
	} else {
		$link_text = '';
	}

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

	/**
	 * Filter 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      $size      Image size. 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', "$link_text", $id, $size, $permalink, $icon, $text );
}

wp_get_attachment_link() 源文件