WordPress教程

WooCommerce 调用商店精选特色产品代码片段

阿里云

WooCommerce 已经是 WordPress 平台最好最强大的在线商城插件,已经被 WordPress 收购,更新速度比较频繁,功能更加完善。本文章主要介绍了 WooCommerce 特色产品循环作为模板标记和短代码以及相关的经验技巧。

在频繁的更新后,一些常用的函数都在优化,一些字段也有所改变。比如获取的是商店特色产品,我们是可以通过以下代码就可以获取特色产品:

  1. <?php
  2. $args = array(
  3. 	'posts_per_page' => -1,
  4. 	'post_type'      => 'product',
  5. 	'post_status'    => 'publish',
  6. 	'tax_query'      => array(
  7. 		array(
  8. 			'taxonomy' => 'product_visibility',
  9. 			'field'    => 'name',
  10. 			'terms'    => 'featured',
  11. 			'operator' => 'IN',
  12. 			),
  13. 		)  
  14. );
  15. $featured_product = new WP_Query( $args );
  16.  
  17. if ( $featured_product->have_posts() ) : 
  18.  
  19. echo '<div class="woocommerce columns-3"><ul class="products">';
  20.  
  21. while ( $featured_product->have_posts() ) : $featured_product->the_post();
  22.  
  23. $post_thumbnail_id     = get_post_thumbnail_id();
  24. $product_thumbnail     = wp_get_attachment_image_src($post_thumbnail_id, $size = 'full');
  25. $product_thumbnail_alt = get_post_meta( $post_thumbnail_id, '_wp_attachment_image_alt', true );
  26. ?>
  27.  
  28. 	<li class="product">
  29. 		<a href="<?php the_permalink();?>">
  30. 			<img src="<?php echo $product_thumbnail[0];?>" alt="<?php echo $product_thumbnail_alt;?>">
  31. 			<h3 class="woocommerce-loop-product__title"><?php the_title();?></h3>
  32. 			<button class="yellow-but">VIEW PRODUCT</button>
  33. 		</a>    
  34. 	</li>
  35. <?php 
  36. endwhile; 
  37.  
  38. echo '</ul></div>';
  39.  
  40. endif; 
  41.  
  42.  wp_reset_query();
  43.  ?>          
  44. <!-- Featured products loop -->
也想出现在这里?联系我们
创客主机

如果你需要在任意地方调用,可以使用 WordPress 的简码 功能,代码如下:

  1. <?php
  2. add_shortcode( 'woo_featured', 'wb_woo_featured' );
  3. /*
  4.  *
  5.  * Featured Product Loop
  6.  */
  7. function wb_woo_featured() {
  8.  
  9. $args = array(  
  10. 	'post_type'      => 'product',  
  11. 	'posts_per_page' => 3,
  12. 	'post_status'    => 'publish',
  13. 	'tax_query'      => array(
  14. 		array(
  15. 			'taxonomy' => 'product_visibility',
  16. 			'field'    => 'name',
  17. 			'terms'    => 'featured',
  18. 			'operator' => 'IN'
  19. 		),  
  20. 	),
  21. );  
  22. $featured_product = new WP_Query( $args );  
  23.  
  24. if ( $featured_product->have_posts() ) :  
  25.  
  26. ob_start();
  27.  
  28. echo '<div class="woocommerce columns-3"><ul class="products">';
  29. while ( $featured_product->have_posts() ) : $featured_product->the_post();
  30.  
  31. $product               = wc_get_product( $featured_product->post->ID );  
  32. $post_thumbnail_id     = get_post_thumbnail_id();
  33. $product_thumbnail     = wp_get_attachment_image_src($post_thumbnail_id, $size = 'shop-feature');
  34. $product_thumbnail_alt = get_post_meta( $post_thumbnail_id, '_wp_attachment_image_alt', true );
  35.  
  36. // Featured Post Loop Output 
  37. //	wc_get_template_part( 'content', 'product' ); 
  38. ?>
  39.  
  40. 	<li class="product">
  41. 		<a href="<?php the_permalink();?>">
  42. 			<img src="<?php echo $product_thumbnail[0];?>" alt="<?php echo $product_thumbnail_alt;?>">
  43. 			<h3 class="woocommerce-loop-product__title"><?php the_title();?></h3>
  44. 			<button class="yellow-but">VIEW PRODUCT</button>
  45. 		</a>    
  46. 	</li>
  47.  
  48. <?php
  49.  
  50. endwhile;  
  51. echo '</ul></div>';
  52. endif;  
  53. wp_reset_query();
  54.  
  55. return ob_get_clean();
  56. }

WooCommerce 调用商店精选特色产品代码片段

已有 176 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!