As you’re trying to get the ID of an attachment, functions like get_post_thumbnail_id()
won’t work on an attachment post because itself is the attachment.
You should be able to get the ID simply with
$post_id = $attachment->ID;
Related Posts:
- Adding a ‘featured’ class to certain posts
- Featured Posts Code – Repeated featured posts
- Filter article that has featured image to shown on homepage [duplicate]
- Show last 5 posts from featured category on query carousel slider with prev and next button
- External URL Link as Featured Images
- How to display the featured image of a parent on child pages?
- Hide featured post from second page
- WordPress : how to allow a user to post a module that leads any visitor to a product page ? (front end)
- Rotate featured post image
- How to have a different section for a special category of posts in landing page?
- Prevent a query from duplicating
- Feature Image Showing Too Big
- Can I exclude a post by meta key using pre_get_posts function?
- Display featured products through custom loop in woocommerce on template page
- Keep featured content post in homepage with original order
- Is “Featured Content” in Twenty Fourteen done with a plugin, or is it native in WP4.0?
- How to display Section for certain time
- Add a single featured post to all pages?
- How to get featured post title & image using JSON API?
- Is there a hook / action that is triggered when adding or removing a post thumbnail?
- XMLRPC and Underscored custom fields
- Featured Image Inherited from Parent Page
- Three different post types on homepage
- featuring old articles without messing up with the archive
- Exclude most recent posts from specific category in wp_query()
- How to change featured content to a different tag in WordPress Twenty Fourteen?
- Automated mark posts as featured every day
- Get the size (size in Kb or MB) of an featured image?
- Broken pagination, need help figuring out why!
- Display featured products through custom loop in WooCommerce 3 [closed]
- Find posts without featured image? [duplicate]
- How to get relative URL of medium sized featured image?
- How to set the width and height rule for featured image
- How to remove sticky post capability for a specific user role?
- Include a specific post to the query_posts and remove it if it is already in the returned list
- Gutenberg editor get post featured image by id
- “Issues” loading MANY featured images on a single page (custom template)
- Exclude posts from homepage having a specified tag
- Get the 5 lastest Featured Posts from a category
- What is the best way mark entries as featured?
- Hide featured post on second page
- How to add pagination to my code?
- how to exclude “featured” posts from the main loop?
- How to check if there is image title, caption, description?
- Front page should display only one category of posts including sticky post with custom featured post box
- Big Image on Featured Post, Normal on Single Post
- How to use photo in multiple posts?
- Display Featured Post by Categories and Avoid duplicated posts
- Media Gallery Upload photo incorrect way like glitch
- Show Featured Post Only On The Homepage
- How do I create a function that simulate the click on Update button for all posts?
- Added Custom Post Type and the View Option Doesn’t Work
- How do I style the first two posts of a loop
- Setup Featured image on all posts from thesis thesis_post_image custom field?
- How to display only feautured/sticky listings on the homepage
- Latest Post Styled Different Than other Posts
- WordPress: Notice:Object of class WP_Query could not be converted to int in on line
- How to automate featured posts number? [duplicate]
- Check if parent post is sticky on attachment page?
- Singleton Custom Field (Meta Box)
- Featured-Content/Featured Posts With Random Order
- Display single most recent post in category on every page of category archive
- How to display the featured post on the category page?
- How do I link a featured photo to it’s post?
- upload image only
- Remove any product that is featured from regular display loop [WooCommerce]
- Get template part with CPT and Custom Taxonomy conditionals
- Show image if a post is featured
- How can I detect if the current post is in this loop?
- How can I specify the width and height of the Featured Image in TwentyTwelve theme?
- show div only if have a related post inside
- handling the featured category
- filter posts without images or featured image
- Need to add a post’s featured image to a section on the frontpage
- Delete media from the post but keep my featured image and post
- How to mark only one post as Featured post?
- Featured images duplicating and replacing next image
- WP site homepage displays url link at beginning of each featured post
- How to filter get previous post function by meta value DESC and post date DESC?
- How can i link my featured image to be clicked and the file will be downloaded
- Add specific class to featured posts
- Using lower resolution/size images for thumbnails on posts index
- how do i make this loop work
- Display Custom Post if custom field is marked
- Why I have this strange behavior when I try to exlude the featured post from posts visualization?
- Featured posts and the loop
- Display thumbnail of child, parent and ancestor using featured thumbnails
- Option to feature custom post type on home page
- Title image is missing ( but is not the WP 3.5 issue )
- Automatic connect wordpress to facebook with featured image?
- How to show featured post first, then separate loop for other posts
- Previous / next posts using featured image thumbnail as links
- Orientation of featured image in post?
- How to show file size of featured image? [duplicate]
- How to show file type of featured image?
- Remove the excerpt and “… Read More” from the featured content on the home page with child theme
- How to list blog post from subdirectory wordpress install to the main site
- Custom Post Type featured option
- How do I change the size of the feature picture of a single post?
- set_post_thumbnail or media_sideload_image adds image multiple times in media library