Came here as this post is among the top hits on the search engine when looking for WordPress image alt and title. Being rather surprised that none of the answers seem to provide a simple solution matching the question’s title I’ll drop what I came up with in the end hoping it helps future readers.
// An attachment/image ID is all that's needed to retrieve its alt and title attributes.
$image_id = get_post_thumbnail_id();
$image_alt = get_post_meta($image_id, '_wp_attachment_image_alt', TRUE);
$image_title = get_the_title($image_id);
As a bonus here’s how to retrieve an image src. With the above attributes that’s all we need to build a static image’s markup.
$size="my-size" // Defaults to 'thumbnail' if omitted.
$image_src = wp_get_attachment_image_src($image_id, $size)[0];
Related Posts:
- How to change _wp_attachment_metadata specific value in SQL?
- How can I get a different image size, if all I have is the link?
- Exclude featured image from attachment loop
- How to create resized versions of images added with wp_insert_attachment
- How to attach images that I “insert from url” into the post?
- How to Fix HTTP Error When Uploading Images?
- How to disable WordPress from creating thumbnails?
- Programmatically get images by URL and save in uploads folder
- How do you get the post thumbnail size?
- set_post_thumbnail_size vs add_image_size
- Minimum Dimensions Requirement for Featured Image?
- Generate Thumbnails only for Featured Images
- Get the first image from post content (eg.: hotlinked images)
- How to get all featured image sizes and their URLs?
- How to Dynamically Resize WordPress Image On-The-Fly (custom field/theme option)
- Display thumbnail only if requested size exists
- Delete original image – keep thumbnail?
- the_post_thumbnail responsive srcset not populating with custom image size
- How to add multiple image sizes of the same size with add_image_size
- New image size version only for the Featured Image file
- Can’t extract and set SVG dimensions
- Set Featured Image Front Frontend Form
- How to find attachment by it’s name?
- media_sideload_image generates blank?
- Getting custom-sized featured image’s URL?
- Showing a placeholder/default img, if no featured image is set
- Insert an image into a post by API
- Featured image with the same height thumbs returns wrong thumb
- How to change the naming of the resized image files
- Change Output for Images in Content
- Programatically creating image attachments from local URLs and setting featured image
- Can WordPress crop only the post thumbnails?
- Allow user to manually set crop area of default/custom image sizes on upload [closed]
- How to get the image sizes, in a function, as per the sizes mentioned in the media settings?
- Adding Featured Image to Post programatically
- Resizing Images to lower Disk Space Using Plugins
- Set (featured) thumbnail for post?
- Autogenerated Thumbnail compression depending on size
- Only create image size when used in featured image
- How to get custom image sizes into media uploader dropdown?
- WordPress resizes image to the same size as uploaded
- add_image_size and add_filter(‘image_size_names_choose’, ‘my_custom_image_sizes’) not working with wordpress 3.5.2
- Show prev and next post links for parent post of current image in attachment page?
- wordpress responsive image srcset not working
- Customising the size of images served in Visual Composer elements [closed]
- How do I get a random image from subset of images in media gallery?
- Delete all thumbnails for a post
- How to set an attachments parent post id in code?
- How to scale up post thumbnails/featured image?
- Why are extra 92 pixel square thumbnails created?
- wp-cli regenerate media is not working for custom sizes
- Upload 3 different featured image sizes to WordPress
- Remove dimensions from filename featured image
- Can a thumbnail of an animated gif be animated?
- Featured Image Size Conditional Tag?
- Converting Attachment to Images?
- WordPress Uploader Editor shows broken image
- add_image_size not working with the_post_thumbnail
- How do i show wordpress attachments from current post?
- Need clear explanation about medias/files/attchments
- Why does the Hard Crop option Scale?
- How do I set the default “feature image” size?
- How to get full absolute url for post attachment?
- Thumbnail Cropping with add_image_size?
- Image still attached to post even after removing it as featured image
- thumbnails are getting clipped down, want to resize instead
- How check that there is post thumbnail feature image or not on wordpress?
- If I Regenerate Thumbnails, would WordPress delete the existing Thumbnails and replace them with ‘new copies’?
- How to get cropped thumbnail source for custom post type
- Set size for attachments
- 2 image size for post
- adaptive featured images wordpress 4.4 grid
- Featured Image Not Working when loaded from sub-domain
- How to write conditional for custom checkbox in featured image meta box
- How can I see where in my themes and plugins certain image sizes are used?
- Generate image for newly added size
- How to both crop (with tool) and resize thumbnails having only a max width constrain?
- “Medium” size image displayed instead of “thumbnail” in only one post, other posts are fine
- wp_get_attachment_image_src match smallest edge
- Show Multiple Size URL of one Featured Image/Thumbnail?
- WordPress returns old thumbnail size after changing media settings
- Custom image size in the_content loop
- remove and disable image link in functions.php
- specify size of post preview image (not post thumbnail)
- Display featured image file size, extension, filename, type, orientation?
- media-library: how to insert thumbnails with a fixed width, regardless of landscape/portrait orientation
- Check if parent post is sticky on attachment page?
- the_post_thumbnail(‘my_image_size’) always outputs full image
- Server B handling add_image_size() differently than Server A
- Post thumbnail throwing size limit parameters in
- Get thumbnail image according to the screen
- Image with large file size not cropping
- What can I do if I set a gif as featured image? no thumbnail when i share on facebook
- How do you import images from a URL in your post?
- images not showing despite using add_theme_support(‘post-thumbnails’);
- Retrieve Image Attachments Getting Post Thumbnail Image First
- How to hide image-url if no attachment?
- Resizing thumbnails retrospectively
- Trying to show link “Use as featured image” from Attachment window from t31os script
- post thumbnail: increase dimension if smaller than… Possible?