The the_attachment_link
returns an HTML link, so use this code:
if ( $attachments = get_children( array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'numberposts' => 1,
'post_status' => null,
'post_parent' => $post->ID
) ) );
foreach ( $attachments as $attachment ) {
echo wp_get_attachment_link(
$attachment->ID, '' , true, false, 'Link to image attachment'
);
}
Here I’m passing 5 parameters to function wp_get_attachment_link()
- First parameter is
$attachment->ID
to get attachment ID - Second parameter
''
says not to print image - Third parameter
true
links to attachment page - Fourth
false
parameter tells not to print media parameter - The last one is the text you’d like to appear as a link
Related Posts:
- Programmatically adding images to the media library with wp_generate_attachment_metadata randomly fails
- How can I bulk delete media and attachments using WP-CLI?
- complex restriction of items in media library
- Edit image / Delete image link
- media_sideload_image file name?
- Get all posts (of any post type) an attachment is used in
- Unattaching unlinked images
- Show Post ID in “Find Posts or Pages” box in Media Library?
- Gel all image from certain post type
- Can i hide the attachments from media which I uploaded from front end?
- Perform a custom (bulk) action in media view
- How to retrieve alt attribute for an attachment (uploaded image)?
- Where does the gallery shortcode gets its attachments from?
- Control attachments menu order with jQuery Sortable
- Is it possible to share media files between multiple WordPress sites?
- Open the Attachment Details popup
- Proper use of wp_get_attachment_image?
- how to get random media id from media gallery
- Upload multiple image with media_handle_upload with multiple file field [duplicate]
- Get an image from the media library. Struggling with the code?
- Media Library Mess After FTP … /uploads
- Media item permalink goes to source file instead of attachment.php
- wp_get_attachment_image_src calling wrong location
- Removing images deleted from uploads folder, from Media Library
- WP media attachment location vs permalink
- Removing delete permanently button in uploading files media
- Retrieve ONLY mp3s and set a variable for use in a playlist called by single.php template?
- Is there a way to add Reattach and Unattach links in image gallery?
- How to retrieve images contained in a post
- How to save media-form on custom tab
- Bulk delete media by year
- Media library upload hook
- Can I add a Category Metabox to attachment?
- How to trigger a refresh in the media modal
- Get all post attachments except featured image
- Changing attachment urls?
- Add custom class to attachment in media library grid mode
- How can I UN-attach media from a post?
- Password protect some uploaded files, so only logged-in users can view them
- How to get attachments for a specific post type?
- How to add media from front-end to an existing post?
- Get attachments (get_posts) and WP 3.5 new uploader
- Make individual attachments private?
- exposing attachment uploading to the front end — delicate
- How to get list of paths (not urls) for an image
- Get post embedded image caption
- How to allow specific extensions and file size to wp_mail attachment?
- Get attachments by user
- attachments with tags and get_posts
- Hide “Gallery Settings” and “Insert into Post” button from Attachment window
- How to add multiple checkbox elements to media attachments?
- Get attachment next and previous by author only
- Edit default image attachment size
- Strip the folder path away from wp_get_attachment_url to show filename only
- Show attached media image if no thumbnail image detected
- How to fetch only media that was already attached to a post/page?
- How to set a conditional statement for $attachment->post_excerpt, to check for value?
- Post edit – Media Library – Only get images from current post
- wp_mail not sending attachment
- How to check if there is image title, caption, description?
- How to remove attachments size attribute [duplicate]
- hook wp_generate_attachment_metadata does not read all the attachments
- Massive photogallery import
- How to load attachment in media library for current user?
- Auto create file and send it via wp_mail()
- Create page from post attachments
- Attach pdf from dynamic url
- Post Gallery list attachments except the one used as post thumbnail
- Echo Permalink in Attachment outside of loop?
- How can I display a link to a post’s (Word doc or pdf) attachment?
- GUID field in wp_posts – relevance for attachments?
- Is there a function for getting the post/page that an attachment is related to?
- Prevent image from being accessible via url as a post?
- get_post orderby not working in plugin
- Delete child post and attachment links to parent post
- Remove attachment from database
- Display Audio Attachment URL
- How to set order for attachment form field?
- changing the default picture size link url
- File Attachment display title in template
- add image caption
- Get all post embedded images
- Programmatically Altering Gallery Link and image size?
- File was uploaded, but it is not shown on media section of the dashboard
- wp_get_attachment_metadata is empty
- Attachments plugin cannot click media library button [closed]
- Restricted Attachments/Change Attachment Upload Location
- Hide Image Container if No Image Attachments
- How to link images to attachment.php [duplicate]
- Check whether attachment ID is associated with more than one post?
- How to remove attachments from main query via pre_get_posts?
- Post attachments doesn’t show after manual db import
- Possible to “Attach” images to multiple posts without uploading twice from Add media Section?
- How to get boolean if file A is attached to post B
- ID of images from gallery
- WordPress Attachments vs Post Meta
- open image after clicking on single.php with different stylesheet in wordpress
- WPML : how to pick a featured image only once for different languages?
- How do I get link URL (not file URL or attachment URL) of an attached image [duplicate]
- Attachment page custom title?