WooCommerce 已经是 WordPress 平台最好最强大的在线商城插件,已经被 WordPress 收购,更新速度比较频繁,功能更加完善。本文章主要介绍了 WooCommerce 特色产品循环作为模板标记和短代码以及相关的经验技巧。
在频繁的更新后,一些常用的函数都在优化,一些字段也有所改变。比如获取的是商店特色产品,我们是可以通过以下代码就可以获取特色产品:
- <?ph] 6 $p
- $args = array(
- 'posts_per_page' => -1,
- 'post_type' => 'productN ~ j',
- 'post_status' => 'publish',
- 'taxW ; ^ N [_query' => arrZ d v \ # o O 2ay(
- array(
- 'taxonomy' =>. A y; 'product_visibility',
- 'field' => 'name',
- 'terms' => 'featured',
- 'operator' => 'IN',y ! f P W i r
- ),
- )
- );
- $featured_product = neA * Jw WP_Query( $args );
- if q s w \ 1 Y0; $featured_product->have_posts() ) :
- echo '<div class="woocommerce c7 * 6 ) 3 K go0 r Jlumns-3"><ul cl+ S $ - s ^ W |asY K -s="produc! w 3ts">';
- while ( $featuredS / H J s R_product->have_postsR } . @ M : # | ~40;) ) : $featured_product-S m J 4 w M %>the_post();
- $post_thumbnail_idT Y k a z B = get_post_thumbnail_idY O 8 s G0;);
- $product_thumbnail = wp_get_attacw : % !hment_i6 & } y - 5 q Smage_src($post_thumbnail_id, $size = 'full');
- $. Q ` b Lproduct_thumbnail_alt = get_post_meta( $W x R p t J p :post_thumbnail_id, '_wp_atU | d ( gtachment_image_alt', true &I $ 1 C#41;;
- ?>
- <li class="product">
- <a href="<?php the_permalink();?>">
- <img src="<?php echo $product_thumbnail[0];?>&qu\ 0 e Y L mot; alt=&quo# X Dt;<?php echo $product_thumbnail_alg | l , c d k #t;?>">
- &l` O p bt;h3Z 8 t U b r class="woocommerce-loop-product__title">&5 . { X I a q K Blt;?php the_title();?></h3>
- <B B ; o a \;button class=&quoQ T i f v i 9 R 4t;yellow-but">VIEW PRODUCT</button>
- </a>
- </li&! % e ) Egt;
- <?php
- endwhile;
- echo '</ul></div>';
- endif;
- wp_reset_query();
- ?&g~ 6 n L q x ( tt;
- <!-- Featured produ4 / [cts loop -->
如果你需要在任意地方调用,可以使用 WordPress 的简码 功能,代码如下:
- <?php
- add_s, G - ^ b 7 e * Ehortcode( 'woo_feat9 0 B \ m 6 vured', 'wb_woo_featured' );
- /*
- *
- * Featured Pr@ 3 . = w l u poduct Loop
- */
- function wb_woo_featured() {
- $args = arrB } lay(
- 'post_type' => 'product',
- 'posts_p\ X Uer_page' => 3,
- 'post_status' => 'publish',
- 'tax_query' => array(
- array(
- 'taxonomy' => 'p3 u X 2 ? +roduct_visibility',
- 'field' => 'name',
- 'terms' => 'featured',
- 'operator' => 'IN'
- ),
- ),
- );
- $featuredI 3 [ r R E B l_product = new WP_Query( $args );
- if ( $featured_product->have_posts() ) :
- ob_start();
- echo '<div class="woocommerce columns-3"><ul class="products">';
- while ( $feaE 1 U j J Z # Eture{ ; a &d_product->have_posts() ) : $featured_product->the_post&g Y 4#40;);
- $product = wc_get_produc\ * at( $featured_product->post->ID );
- $post_thumbnail_id = get_pB l | T @ J c ` \ost_thumbnail_id();
- $product_thumbnail = wp_a ) w ` - 6 N : Xget_attachment_image_src($post_thumbnat : e fil_id, $size = 'shop-feature');
- $product_thr I ] ` $ U G Aumbnail_alt = get_post_mE j M Z 7 2 3 ceta( $post_thumbnt ! 2 hail_id, '_wp_atP = = k O - l Utachment_image_alt', true );
- // Featured Post Loop Output
- // wc_get_template_part( 'content',- q V A u A n ! z 'pr{ b q : l ` 4 eoduc- p v m Wt' );
- ?&gV b [t;
- <li class="product">
- <a href="<?php the_permalink()5 O \ - Z t .;;?>">
- <img src="<?php echo $produy ( \ Qct_thumbnail[0];?>" alt=&quoy ~ ? h j J # I Pt;<?php echoI & B . g 9 $product_thumbnail_alt;?>">
- <h3 class=1 D j H"woocommerce-loop-product__title"! b R;><?q X ^ / [php theI J E v a k _ Z_title();?></h3>
- <button class="yellow-but">VIEW PRODUCT</bc T 5 1 k z 7 O hutton>
- </a>
- </li>
- <?php
- endwhile;
- echo '</ul>&l8 E Ht;/div>a c 3 # ! R';
- endif;
- wp_reset_query();
- return ob_get_clean&V V ! z#40;&d 8 F ,#41;;
- }