get_attachment_link( int|object $post = null, bool $leavename = false )

Retrieves the permalink for an attachment.


Description Description

This can be used in the WordPress Loop or outside of it.


Top ↑

Parameters Parameters

$post

(int|object) (Optional) Post ID or object. Default uses the global $post.

Default value: null

$leavename

(bool) (Optional) Whether to keep the page name.

Default value: false


Top ↑

Return Return

(string) The attachment permalink.


Top ↑

More Information More Information

Under a “pretty” permalink structure, the function returns something like http://wp.example.net/path_to_post/post_name/attachment_name.

Under the default permalink structure — or if WordPress can’t construct a pretty URI — the function returns something like http://wp.example.net/?attachment_id=n, where n is the attachment ID number.

You can change the output of this function through the attachment_link filter.

If you want a direct link to the attached file (instead of the attachment page), you can use the function wp_get_attachment_url(id) instead.

Note: that get_attachment_link actually returns an URI, whereas wp_get_attachment_link() returns an HTML hyperlink.


Top ↑

Source Source

File: wp-includes/link-template.php

function get_attachment_link( $post = null, $leavename = false ) {
	global $wp_rewrite;

	$link = false;

	$post   = get_post( $post );
	$parent = ( $post->post_parent > 0 && $post->post_parent != $post->ID ) ? get_post( $post->post_parent ) : false;
	if ( $parent && ! in_array( $parent->post_type, get_post_types(), true ) ) {
		$parent = false;
	}

	if ( $wp_rewrite->using_permalinks() && $parent ) {
		if ( 'page' === $parent->post_type ) {
			$parentlink = _get_page_link( $post->post_parent ); // Ignores page_on_front.
		} else {
			$parentlink = get_permalink( $post->post_parent );
		}

		if ( is_numeric( $post->post_name ) || false !== strpos( get_option( 'permalink_structure' ), '%category%' ) ) {
			$name = 'attachment/' . $post->post_name; // <permalink>/<int>/ is paged so we use the explicit attachment marker.
		} else {
			$name = $post->post_name;
		}

		if ( strpos( $parentlink, '?' ) === false ) {
			$link = user_trailingslashit( trailingslashit( $parentlink ) . '%postname%' );
		}

		if ( ! $leavename ) {
			$link = str_replace( '%postname%', $name, $link );
		}
	} elseif ( $wp_rewrite->using_permalinks() && ! $leavename ) {
		$link = home_url( user_trailingslashit( $post->post_name ) );
	}

	if ( ! $link ) {
		$link = home_url( '/?attachment_id=' . $post->ID );
	}

	/**
	 * Filters the permalink for an attachment.
	 *
	 * @since 2.0.0
	 *
	 * @param string $link    The attachment's permalink.
	 * @param int    $post_id Attachment ID.
	 */
	return apply_filters( 'attachment_link', $link, $post->ID );
}


Top ↑

Changelog Changelog

Changelog
Version Description
2.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Codex

    Default Usage
    As the tag does not display the permalink, the example uses the PHP echo command.

    <?php 
    $attachment_id = 1; // ID of attachment
    $attachment_page = get_attachment_link( $attachment_id ); 
    ?>
    <a href="<?php echo esc_url( $attachment_page ); ?>"><?php echo get_the_title( $attachment_id ); ?></a>
    
    
  2. Skip to note 2 content
    Contributed by Codex

    Display attached images and titles as a list
    To display the images attached to a certain page and display them as a list of bullets you can use the following:

    <ul>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post();    
    
    	$args = array(
    		'post_type'   => 'attachment',
    		'numberposts' => -1,
    		'post_status' => null,
    		'post_parent' => $post->ID
    	);
    
    	$attachments = get_posts( $args );
    	if ( $attachments ) {
    		foreach ( $attachments as $attachment ) {
    			echo '<li>';
    			the_attachment_link( $attachment->ID, true );
    			echo '<p>';
    			echo apply_filters( 'the_title', $attachment->post_title );
    			echo '</p></li>';
    		}
    	}
    endwhile; endif; ?>
    </ul>
    

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