Victor Sokoliuk
Victor Sokoliuk

Reputation: 445

The link from Woocommerce product does not go to a single product, it just reloads the home page

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. enter image description here I don’t understand why this is happening, help me figure it out.

Upvotes: 0

Views: 120

Answers (1)

Alice
Alice

Reputation: 1433

use get_permalink($post) instead of use get_permalink();

Upvotes: 2

Related Questions