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 Fix HTTP Error When Uploading Images?
- Can I add a Category Metabox to attachment?
- Change attachment filename
- How to get attachment file name not attachment URL
- How to use taxonomies on attachments with the new Media Library?
- Get all post attachments except featured image
- Programmatically adding images to the media library with wp_generate_attachment_metadata randomly fails
- getting attachement images src and add classes
- Getting a taxonomy’s thumbnail url
- Stop WordPress from reserving slugs for media items?
- Check if post has attachments (not image)
- How to cleanly delete attachment but retain original file?
- Changing attachment urls?
- Can I use the wp media uploader for my own plugin?
- Add custom class to attachment in media library grid mode
- Open media frame and select an attachment
- Are captions stored anywhere?
- Trying to hide buttons from Attachment window
- get_the_post_thumbnail() doesn’t taking style attribute
- media_handle_upload for local files?
- How can I UN-attach media from a post?
- Put the media uploader in a metabox
- Validate alt text for attachments?
- get_attached_media() returns empty array if media file already used by another post
- complex restriction of items in media library
- Remove duplicate attachments
- Edit image / Delete image link
- How could I add button next to “edit image” button in “Attachment Details”
- Applying automatic link class to images embedded to posts
- How do I detach images from posts?
- How to get attachment caption (get_the_excerpt gives parent post excerpt) ?
- Settings in ‘Media > Settings’ is ignored when inserting images
- Is it possible to query specific WordPress Attachment files (.ppt & .pdf) and output them in a list?
- How do I delete all generated images from my server except those currently used in posts
- media_sideload_image file name?
- How to get all files inserted (but not attached) to a post
- Custom function to rearrange attachments when creating posts – Almost there
- Detect if is image unattached
- Password protect some uploaded files, so only logged-in users can view them
- Replacing all attachment links in post with media file link
- delete attachment from front end
- How to get attachments for a specific post type?
- Give attachments an archive page
- custom fields for attachments?
- Insert Media – Attachment – Link to : Remove the “attachment page” option
- Show Post ID in “Find Posts or Pages” box in Media Library?
- URL rewrite with add_rewrite_rule and attachment_id
- How to add media from front-end to an existing post?
- Disable slow media queries?
- How to set author for post AND post attachments
- Retrieving a custom link on an attachment
- Get attachment from post
- How can I display custom fields value from attachment media?
- Add column for attachment file size
- Creating a multi-file upload form on the front end for attachments
- how to test for attached image
- How do i upload an image and return the image id?
- Is it possible to generate JPEG progressive thumbnails?
- Unable to upload specific .zip file type via frontend in WordPress 4.0
- get post attachments of a particular size
- Change image link: wp_get_attachment_link
- Multi-line captions on attachments
- Make individual attachments private?
- exposing attachment uploading to the front end — delicate
- Gel all image from certain post type
- Making images from single.php pointing to an attachment .php template