Woocommerce get product id outside loop
I would like to display 6 featured products from my woocommerce store on my home-page.php template. After some researched I found that the right way to do this was through a custom loop,( I do not wish to use shortcodes because I would like to add additional classes for styling etc. ) I also found that the key that woocommerce uses for the featured products is ‘_featured’. I put together the code below to display any products that I chose to be featured products in my store, but it doesn’t work. Any help is appreciated.
Change your args to be like this:
If you go to wp-content/plugins/woocommerce/includes/class-wc-shortcodes.php (@595) you can find how it’s done for WC shortcodes.
This has changed in WooCommerce 3.0. It’s not simply a meta_query, but now includes a tax_query. The arguments are now:
get_the_post_thumbnail_url( int|WP_Post $post = null , string|array $size = ‘post-thumbnail’ )
Return the post thumbnail URL.
Description # Description
Parameters # Parameters
( int | WP_Post ) (Optional) Post ID or WP_Post object. Default is global $post .
Default value: null
( string | array ) (Optional) Registered image size to retrieve the source for or a flat array of height and width dimensions.
Default value: ‘post-thumbnail’
Return # Return
(string|false) Post thumbnail URL or false if no URL is available.
Source # Source
Changelog # Changelog
Related # Related
Uses # Uses
Get the URL of an image attachment.
Retrieve post thumbnail ID.
Used By # Used By
Display the post thumbnail URL.
User Contributed Notes # User Contributed Notes
Don’t ignore the first parameter.
Proper usage of `get_the_post_thumbnail_url()` inside the loop:
Downvoted the example posted from @thelilmercoder as it is not proper usage of this function.
Proper usage of ` get_the_post_thumbnail_url() ` inside the loop:
Proper usage of ` get_the_post_thumbnail_url() ` outside the loop:
It’s worth to note that, if you upload a smaller image (let’s say, a 600px wide) and use this to fetch a specific larger image (let’s say, a 1920px wide for your cover), it will return the original image instead (which is smaller than what you need) instead of returning false.
If you need to fallback to another image in case the specified file doesn’t exist, you can look into wp_get_attachment_image_src instead, and check for the width or height of the image.
To display the featured image with the alt tag use something like this
You must log in before being able to contribute a note or feedback.