^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 8年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云2核2g仅需86元/年

wordpress函数get_attachment_link()用法示例

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

Retrieves the permalink for an attachment.


描述

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

新用户2核2g仅需86元/年    领取1153套模板    免费cdn流量包    ¥3188礼包


参数

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

更新日志

Version 描述
2.0.0 Introduced.

相关函数

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


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


  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>
    

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈