Advertisements

get_attachment_link

Definition:
function get_attachment_link($id = false) {}

Retrieve permalink for attachment.
This can be used in the WordPress Loop or outside of it.

Parameters

  • int $id: Optional. Post ID.

Defined filters

  • attachment_link
    apply_filters('attachment_link', $link, $id)

Source code

function get_attachment_link($id = false) {

	global $post, $wp_rewrite;



	$link = false;



	if ( ! $id)

		$id = (int) $post->ID;



	$object = get_post($id);

	if ( $wp_rewrite->using_permalinks() && ($object->post_parent > 0) && ($object->post_parent != $id) ) {

		$parent = get_post($object->post_parent);

		if ( 'page' == $parent->post_type )

			$parentlink = _get_page_link( $object->post_parent ); // Ignores page_on_front

		else

			$parentlink = get_permalink( $object->post_parent );



		if ( is_numeric($object->post_name) || false !== strpos(get_option('permalink_structure'), '%category%') )

			$name = 'attachment/' . $object->post_name; // <permalink>/<int>/ is paged so we use the explicit attachment marker

		else

			$name = $object->post_name;



		if ( strpos($parentlink, '?') === false )

			$link = user_trailingslashit( trailingslashit($parentlink) . $name );

	}



	if ( ! $link )

		$link = home_url( "/?attachment_id=$id" );



	return apply_filters('attachment_link', $link, $id);

}

1160

Advertisements

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: