将Woocommerce产品图片和“添加到购物车”链接到产品页面

时间:2016-04-18 08:31:30

标签: php html wordpress woocommerce

我在主页上的网站上设置了一个特色产品区域,目前工作正常,但我也想让图片可点击,一旦点击我希望他们转到产品页面自。另外,我希望woocommerce_template_loop_add_to_cart在添加到购物车后也会转到产品页面。

我的代码:

 <div class="featured-products">
               <h1 class="featured-products">Featured Products</h1>
               <?php
                $meta_query   = WC()->query->get_meta_query();
                $meta_query[] = array(
                    'key'   => '_featured',
                    'value' => 'yes'
                );
                $args = array(
                    'post_type'   =>  'product',
                    'stock'       =>  1,
                    'showposts'   =>  4,
                    'orderby'     =>  'date',
                    'order'       =>  'DESC',
                    'meta_query'  =>  $meta_query
                );

                $loop = new WP_Query( $args );
                while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>

                    <li>    
                        <?php 
                            if ( has_post_thumbnail( $loop->post->ID ) ) 
                                echo get_the_post_thumbnail( $loop->post->ID, 'shop_catalog' ); 

                            else 
                                echo '<img src="' . woocommerce_placeholder_img_src() . ' />'; 
                        ?>
                        <p><?php the_title(); ?></p>

                        <p><?php echo $product->get_price_html(); ?></p>

                        <?php 
                            woocommerce_template_loop_add_to_cart( $loop->post, $product );
                        ?>    
                    </li>
            <?php 
                endwhile;
                wp_reset_query(); 
            ?>

谢谢!

1 个答案:

答案 0 :(得分:0)

更改此

echo get_the_post_thumbnail( $loop->post->ID, 'shop_catalog' ); 

到这个

echo sprintf('<a href="%s">%s</a>', get_the_permalink( $loop->post->ID ), get_the_post_thumbnail( $loop->post->ID, 'shop_catalog' ) );