If you want to use get_attached_media()
, you can try for example:
if( has_post_thumbnail() )
{
the_post_thumbnail();
}
else
{
$imgs = get_attached_media( 'image' );
if( count( $imgs ) > 0 )
{
$img = array_shift( $imgs );
echo wp_get_attachment_image( $img->ID, 'thumbnail' );
}
}
to display one of the attached images, if there’s no featured
image available for the current post.
Notice that you can’t use echo
to show the content of an array, only for a scalar variable.
Related Posts:
- How to manage attachment relationships for specific posts in WP 3.5+
- How to trigger a refresh in the media modal
- How can I bulk delete media and attachments using WP-CLI?
- Expanding new Media Uploader in WordPress 3.5
- Uploading Multiple Attachments From Front-End With A Description
- Get all posts (of any post type) an attachment is used in
- How to single click to download image in single post
- Unattaching unlinked images
- Can I attach a document (eg: PDF) but have a JPEG as a thumbnail?
- Delete attachments from Front end
- Get attachments (get_posts) and WP 3.5 new uploader
- How to count media attachments?
- How to make a customized “Create Gallery” tab in the ‘Add Media’ window?
- How to retrieve alt attribute for an attachment (uploaded image)?
- Get the path of the first attached media (single.php)
- media ‘path’ issue / concern when bringing files live
- Media library quick/inline edit
- hook wp_generate_attachment_metadata does not read all the attachments
- How to compress image before inserting to wordpress using wp_insert_attachment function?
- Massive photogallery import
- What is the best way to store thumbnails for a non-image attachment?
- Delete associated thumbnails when calling wp_delete_attachment
- the_time() returning wrong date/time (way in the future)
- Post Gallery list attachments except the one used as post thumbnail
- Gutenberg Block: Image resolution
- Echo list attachment size in post
- How to get thumbnail from post attachment in this loop
- Media Library Mess After FTP … /uploads
- How to view exact image dimensions of a media in Admin view?
- wp_delete_attachment doesn’t delete image files / doesn’t work
- Setting an image as post header (not featured image)
- Using the GetMediaLibrary() method in python-wordpress-xmlrpc.media, how to iterate over the result?
- Remove attached media
- How To Retrieve An Image Attachment’s Alt Text?
- How to get attachment file name not attachment URL
- How to cleanly delete attachment but retain original file?
- Trying to hide buttons from Attachment window
- Replacing all attachment links in post with media file link
- How can I display custom fields value from attachment media?
- how to test for attached image
- Unable to upload specific .zip file type via frontend in WordPress 4.0
- Add_Filter example for wp_get_attachment_link
- Can i hide the attachments from media which I uploaded from front end?
- Automatically delete attachments and posts [closed]
- How to delete orphan attachments?
- Add a header before fields added with the attachment_fields_to_edit() filter
- Get the title and URL of the attachment parent post
- Confused with attachment mimetype order
- Disable edit media attachments?
- Post Auto Draft Issue
- Generated media have incorrect aspect ratio
- rename attachments based on parent-post-title on upload
- Rename attachments during upload no matter what filetype
- Easy way to delete 70k posts and attached media?
- Is there a way to get attachment data?
- Link all WordPress images size
- post_author for wp_insert_attachment
- Displaying Post Attachment(s) at Top of single.php
- Images not being generated at correct size
- custom admin thumbnail for videos, not getting post’s id?
- Image Gallery Numbers for 3.5
- How to delete a custom file from upload directory when deleting its post
- Attachment page 404
- Query to only return the 2 newest post attachments (images)?
- Clean up images, delete unused image sizes attached to a post type
- Change default selection for “Embed or Link”?
- What will happen if I delete all my attachment type posts from database?
- How can I make an attachment page for pdf uploads?
- filter get_children to return all mime types EXCEPT ‘x’
- Weird string offset errors when displaying post attachments
- Links to my uploads went crazy
- How does WordPress decides how many sizes of an image to create?
- hide attached images of a post
- How to add a caption to an image attachment in a file list?
- How do I intercept and modify the functionality of wp_get_attachment_image()?
- Possible to add “Even/Odd Classes” to image attachments via wp_get_attachment_image?
- Insert attachments from custom uploader into post (regular uploader style)
- Can’t sort get_posts by post_mime_type
- How to list “invisible” Auto Drafts?
- Attachments without images
- How to add media from front-end to an existing post and edit file title
- Add a word to the first // wp_get_attachment_url // as the subdomain name
- How is it possible that a gallery doesn’t have attachment ids?
- How to restrict add media library only to images of the same post family?
- Media item permalink goes to source file instead of attachment.php
- Attachment file display link text
- Limit attachment caption characters
- Get video attachment ID and display length
- Insert attachments from front end form
- controlling whether upload is attached to post or not
- How do I add Facebook OpenGraph meta tags for attachments in the header for a custom theme?
- Create a custom attachment thumbnail for quick page load
- Add link to attachment for old posts
- Media Attachment Custom Meta Fields not saving in Media Uploader when using jQuery UI Autocomplete
- Drupal 6 to WordPress migration – media attachments
- Move caption in on attachment page
- Bulk delete media by year
- Count images in post then add class if just one
- No attachments on contact form 7
- wp_mail attachment not working in wordpress