Reputation: 445
on the main page of the WordPress Woocommerce site I need to display block with 4 products. For this, I use this code:
<?php
global $product;
$args = array(
'post_type' => 'product',
'posts_per_page' => 4
);
$posts = get_posts( $args );
foreach( $posts as $post ) :
setup_postdata( $post );
wc_setup_product_data( $post );
$product = wc_get_product( $post->ID ); ?>
<div id="post-<?php the_ID() ?>" class="three columns product-post">
<figure class="featured-image">
<a href="<?php the_permalink()?>" ><?php echo woocommerce_get_product_thumbnail(); ?></a>
<?php if($product->is_on_sale()): ?>
<span class="onsale__soldout">מבצע</span>
<?php endif; ?>
</figure>
<a href="<?php echo get_permalink(); ?>"> <h2 class="home_prod-title"><?php echo $product-
>get_title(); ?></h2></a>
<h2 class="product-price"><a href="<?php the_permalink(); ?>"><?php wc_get_template( 'single-
product/price.php' ); ?></a></h2>
<div class="add-to-cart-btn">
<?php woocommerce_template_loop_add_to_cart(); ?>
</div>
</div>
<?php endforeach; ?>
And it works. But the link to the product does not work. Instead, the link to the main page created. I don’t understand why this is happening, help me figure it out.
Upvotes: 0
Views: 120