当サイト、Codex 日本語版は今後積極的な更新は行わない予定です。後継となる新ユーザーマニュアルは、https://ja.wordpress.org/support/ にあります。
万が一、当サイトで重大な問題を発見した際などは、フォーラムWordSlack #docs チャンネルでお知らせください。</p>

「テンプレートタグ/wp get attachment link」の版間の差分

提供: WordPress Codex 日本語版
移動先: 案内検索
(英語版をコピペ)
 
1行目: 1行目:
 
== Description ==
 
== Description ==
  
Returns an HTML image element representing an attachment file, if there is any, otherwise an empty string.
+
Returns an HTML hyperlink to an attachment file or page, containing either
 +
 
 +
# An image at some specified size, for image attachments; or
 +
# A media icon (as specified) representing the attachment; or
 +
# The attachment's title (as text) or
 +
# Your own text
 +
 
 +
If no such attachment exists, the function returns the string <tt>Missing Attachment</tt>.
  
 
== Usage ==
 
== Usage ==
  
<?php wp_get_attachment_image( $attachment_id, $size, $icon, $attr ); ?>
+
%%%<?php wp_get_attachment_link( $id, $size, $permalink, $icon, $text ); ?>%%%
  
 
== Default Usage ==
 
== Default Usage ==
  
  <?php echo wp_get_attachment_image( 1 ); ?>
+
  <?php echo wp_get_attachment_link( 13 ); ?>
  
If the attachment is an image, the function returns an image at the specified size.
+
To get attachment IDs dynamically in a template, you would probably use something like <tt>[[Function_Reference/get_children|get_children()]]</tt>.
For other attachments, the function returns a media icon if the <var>$icon</var> parameter is set to <tt>true</tt>.
+
 
+
To get attachment IDs dynamically in a template, you can use [[Template_Tags/get_posts|get_posts('post_type=attachment')]], etc.
+
  
 
== Parameters ==
 
== Parameters ==
 
+
{{Parameter|$id|integer|ID of the desired attachment.|Optional|The current post ID, when used in [[The Loop]] (Must be a loop showing only attachments).}}
{{Parameter|$attachment_id|integer|ID of the desired attachment.}}
+
 
+
 
{{Parameter|$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.|Optional|'thumbnail'}}
 
{{Parameter|$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.|Optional|'thumbnail'}}
 
+
{{Parameter|$permalink|boolean|Link directly to the attachment file/image (<tt>Default</tt>), or to the attachment page. |Optional|'False'}}
Instead of using an array which requires checking all of the image sizes, you should consider registering a size with <code>add_image_size</code> so that a cropped version is generated.  It's much more efficient than having to find the closest sized image.
+
:* <tt>1 (True)</tt>
 
+
:* <tt>0 (False)</tt> - Default
 
{{Parameter|$icon|boolean|Use a media icon to represent the attachment.|Optional|'False'}}
 
{{Parameter|$icon|boolean|Use a media icon to represent the attachment.|Optional|'False'}}
 
:* <tt>1 (True)</tt>  
 
:* <tt>1 (True)</tt>  
 
:* <tt>0 (False)</tt> - Default
 
:* <tt>0 (False)</tt> - Default
 +
{{Parameter|$text|string|Displays a text link to the attachment.|Optional|'false'}}
 +
==Examples==
  
{{Parameter|$attr|string/array|Query string or array of attributes.|Optional}}
+
=== Show Medium Size Attachment. ===
<pre>
+
$default_attr = array(
+
'src' => $src,
+
'class' => "attachment-$size",
+
'alt'  => trim(strip_tags( get_post_meta($attachment_id, '_wp_attachment_image_alt', true) )),
+
);
+
</pre>
+
  
The <tt>$attr</tt> argument is merged with WordPress's default attributes and passed through the <tt>wp_get_attachment_image_attributes</tt> filter.
+
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 [[Settings Media SubPanel|Media panel]] under Settings > Media.
  
== Examples ==
+
<pre><?php
=== Display all images as a list ===
+
    $id = 9; // ID of an attachment
 +
    echo wp_get_attachment_link( $id, 'medium' );
 +
?></pre>
  
To display all of the images and titles attached to a certain page and display them as a list of bullets you can use the following:
+
=== Link Attachment to Post ===
 +
This example will link the attachment to an attachment Page.
  
<pre><ul>
+
<pre><?php  
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();  
+
    $id = 9; // ID of an attachment
 +
    echo wp_get_attachment_link( $id, 'thumbnail', true );  
 +
?></pre>
  
$args = array(
+
=== Link Text to Attachment ===
  'post_type' => 'attachment',
+
  'numberposts' => -1,
+
  'post_status' => null,
+
  'post_parent' => $post->ID
+
  );
+
  
  $attachments = get_posts( $args );
+
This example returns an HTML hyperlink with "My link text" linking to an attachment file.
    if ( $attachments ) {
+
        foreach ( $attachments as $attachment ) {
+
          echo '<li>';
+
          echo wp_get_attachment_image( $attachment->ID, 'full' );
+
          echo '<p>';
+
          echo apply_filters( 'the_title', $attachment->post_title );
+
          echo '</p></li>';
+
          }
+
    }
+
  
  endwhile; endif; ?>
+
  <pre><?php
</ul></pre>
+
    $id = 9; // ID of an attachment
 +
    echo wp_get_attachment_link( $id, '' , false, false, 'My link text' );
 +
?></pre>
  
== Return Value ==
+
=== Link Post Title to Attachment ===
an HTML img element or empty string on failure.
+
 
 +
This example returns an HTML hyperlink with the post title linking to an attachment file.
 +
 
 +
<pre><?php
 +
    $id = 9; // ID of an attachment
 +
    echo wp_get_attachment_link( $id, '' );
 +
?></pre>
 +
 
 +
=== Change Icon Directory ===
 +
 
 +
WordPress can use media icons to represent [[Using_Image_and_File_Attachments|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 [[Theme_Development#Functions_File|functions.php]] file:
 +
 
 +
<pre>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';
 +
}
 +
</pre>
 +
 
 +
== Notes ==
 +
 
 +
Use [[Function_Reference/wp_get_attachment_image|wp_get_attachment_image()]] if you want the image only (not a hyperlink).
  
 
== Change Log ==
 
== Change Log ==
76行目: 94行目:
 
== Source File ==
 
== Source File ==
  
<tt>wp_get_attachment_image()</tt> is located in {{Trac|wp-includes/media.php}}.
+
<tt>wp_get_attachment_link()</tt> is located in {{Trac|wp-includes/post-template.php}}.
  
 
== Related ==
 
== Related ==
 
 
{{Attachment Tags}}
 
{{Attachment Tags}}
  
{{Tag Footer}}
+
[[Category:Template Tags]]
 
+
[[Category:Functions]]
+
[[Category:New page created]]
+

2013年6月22日 (土) 18:52時点における版

Description

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.

Usage

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

Default Usage

<?php echo wp_get_attachment_link( 13 ); ?>

To get attachment IDs dynamically in a template, you would probably use something like get_children().

Parameters

$id
integer) (Optional) ID of the desired attachment.
初期値: The current post ID, when used in The Loop (Must be a loop showing only attachments).
$size
string/array) (Optional) 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.
初期値: 'thumbnail'
$permalink
boolean) (Optional) Link directly to the attachment file/image (Default), or to the attachment page.
初期値: 'False'
  • 1 (True)
  • 0 (False) - Default
$icon
boolean) (Optional) Use a media icon to represent the attachment.
初期値: 'False'
  • 1 (True)
  • 0 (False) - Default
$text
string) (Optional) Displays a text link to the attachment.
初期値: 'false'

Examples

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' ); 
?>

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', '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'; 
}

Notes

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

Change Log

Since: 2.5.0

Source File

wp_get_attachment_link() is located in wp-includes/post-template.php.

Related

添付ファイル関数: get_children(), get attached media(), the_attachment_link(), get_attachment_link(), wp_get_attachment_link(), wp_get_attachment_image(), wp_get_attachment_image_src(), wp_get_attachment_url(), wp_get_attachment_thumb_file(), wp_get_attachment_thumb_url(), is_attachment(), wp_get_attachment_metadata()