返回介绍

get_attachment_link()

发布于 2017-09-10 22:46:22 字数 5875 浏览 924 评论 0 收藏 0

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

Retrieves the permalink for an attachment.


description

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


参数

$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


返回值

(string) The attachment permalink.


源代码

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() ) ) {
		$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 );
}

更新日志

Versiondescription
2.0.0Introduced.

相关函数

Uses

  • wp-includes/formatting.php: trailingslashit()
  • wp-includes/link-template.php: home_url()
  • wp-includes/link-template.php: _get_page_link()
  • wp-includes/link-template.php: get_permalink()
  • wp-includes/link-template.php: attachment_link
  • wp-includes/link-template.php: user_trailingslashit()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/option.php: get_option()
  • wp-includes/post.php: get_post()
  • wp-includes/post.php: get_post_types()
  • wp-includes/class-wp-rewrite.php: WP_Rewrite::using_permalinks()
  • Show 6 more uses Hide more uses

Used By

  • wp-includes/widgets/class-wp-widget-media-image.php: WP_Widget_Media_Image::render_media()
  • wp-admin/includes/media.php: image_media_send_to_editor()
  • wp-admin/includes/media.php: media_upload_form_handler()
  • wp-admin/includes/media.php: image_link_input_fields()
  • wp-admin/includes/post.php: _fix_attachment_links()
  • wp-admin/includes/ajax-actions.php: wp_ajax_send_attachment_to_editor()
  • wp-includes/deprecated.php: get_the_attachment_link()
  • wp-includes/link-template.php: get_permalink()
  • wp-includes/post-template.php: wp_get_attachment_link()
  • wp-includes/media.php: wp_prepare_attachment_for_js()
  • wp-includes/canonical.php: redirect_canonical()
  • Show 6 more used by Hide more used by

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

    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. 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>
    

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

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

发布评论

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