I am trying to display 6 featured products with their thumbnails, price and title. But I am not able to see anything, however if I use $loop->found_posts
I can see there are 6 records fetching back from database.
I have also added these lines in wp-config.php
to see the errors but I am not able to see any error on the page
wp-config.php
define('WP_DEBUG', true);
if (WP_DEBUG) {
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', true);
@ini_set('display_errors', 0);
}
here is my code for displaying featured posts
<?php
$args = array (
'post_type'=>'product',
'meta_key'=>'_featured',
'posts_per_page'=>6
);
$loop= new WP_Query($args);
//echo $loop->found_posts;
while($loop->have_posts()): the_post();
echo '
<div class="col-xs-4">
<div class="custom-product">
<img src="'.woocommerce_get_product_thumbnail(300,335).'">
<div class="price-title">
<h2>'.the_title().'</h2>
<h3>'.$product->get_price_html().'</h3>
</div>
</div>
</div>
';
endwhile;
?>
Few suggestions here:
You should use:
instead of:
because otherwise you are setting up posts related to the global
$wp_query
object.Note that
the_title()
doesn’t return the value, it isecho
-ing it.Make sure
$product
is defined in your loop.Use
wp_reset_postdata()
after your secondary query, to restore the global$post
variable.You can use the
posts
property ofWP_Query
:to peek into the array of queried posts.
To check the generated SQL query, we can use the
request
property ofWP_Query
:This can come handy when we need to debug our query.
This should work as expected.
woocommerce_get_product_thumbnail
echoes the image tag HTML, what you need to pass as parameter is an existing thumbnail name, and width and height of the placeholder.You can set custom image dimensions as stated here -> WooCommerce Codex
shop_catalog My first hit on google returned the following link.
Based on their instructions i updated your code.
The following code should work:
I got the same problem. Try this ! Works for me