2

我正尝试使用Woocommerce类别分类法在页面上显示相关产品。自定义帖子类型允许我将产品类别列表添加到自定义页面,但我不确定如何过滤它以仅显示我为相应页面选择的类别。目前,它正显示出我所有的产品,而不是过滤我需要的:自定义帖子类型中的WooCommerce产品

$args = array('post_type' => 'product', 'posts_per_page' => -1, 'taxonomy' => 'product_cat'); 

$loop = new WP_Query($args); 

while ($loop->have_posts()) : $loop->the_post(); 
    global $product; 
    echo '<div class="background-img"><a href="'.get_permalink().'">' . woocommerce_get_product_thumbnail().'<br /> '.get_the_title().'</a>'; 
    echo $product->get_price_html(); 
    echo '<form class="cart" method="post" enctype="multipart/form-data"> 
    <input type="hidden" name="add-to-cart" value="'; 
    echo esc_attr($product->id); 
    echo '"> 
    <button type="submit">'; 
    echo $product->single_add_to_cart_text(); 
    echo '</button> 
     </form>'; 
    echo '</div>'; 
endwhile; 

$attachment_ids = $product->get_gallery_attachment_ids(); 

foreach($attachment_ids as $attachment_id) 
{ 
    echo $image_link = wp_get_attachment_url($attachment_id); 

} 
wp_reset_query(); 

这里是开发站点的一个版本,它仍然是相当原始,需要造型很多,但应该给一个想法:

http://betamarine.mainboard.com/engine/beta-14-z482/

我使用以下插件:

https://wordpress.org/plugins/custom-post-type-ui/

我可以创建自定义的TA xonomy,但它是一个额外的步骤,并产生相同的结果。我想我可能会错过一些小事,但我只是没有得到它。

回答

1

所以是代码片段:

<?php 
 

 
    //retrieves the term variable from the admin page - replace "product_category" with the name of your post type 
 
$part_terms = get_the_terms($post->ID, 'product_category'); 
 
if($part_terms && !is_wp_error($part_terms)) { 
 
    foreach($part_terms as $term) { 
 
    } 
 
} 
 
//create a variable to filter your Wordpress Loop 
 
    $part_args = array( 
 
\t \t 'post_type' => 'product', 
 
\t \t 'hierarchical' => true, 
 
\t \t 'posts_per_page' => -1, 
 
\t \t 'tax_query' => array(array(
 
\t \t \t 'taxonomy' => 'product_category', 
 
\t \t \t 'field' => 'slug', 
 
\t \t \t 'terms' => array($term->slug), 
 
\t \t \t 'operator' => 'IN' 
 
\t \t \t)) 
 
\t); 
 

 
\t \t \t \t \t \t \t 
 
    $loop = new WP_Query($part_args); 
 
//the loop 
 
    while ($loop->have_posts()) : $loop->the_post(); 
 
    global $product; 
 
//some handy woocommerce coding 
 
\t echo '<div class="background-img"><a href="'.get_permalink().'">' . woocommerce_get_product_thumbnail().'<br /> '.get_the_title().'</a>'; 
 
\t echo $product->get_price_html(); 
 
\t echo '<form class="cart" method="post" enctype="multipart/form-data"> 
 
    <input type="hidden" name="add-to-cart" value="'; 
 
\t echo esc_attr($product->id); 
 
\t echo '"> 
 
    <button type="submit">'; 
 
\t echo $product->single_add_to_cart_text(); 
 
\t echo '</button> 
 
\t \t </form>'; 
 
\t echo '</div>'; 
 
     \t endwhile; 
 

 
?>

感谢JayDeep设置我在正确的轨道上!

1

您需要使用tax_query按照texonomy进行过滤。

检查此链接:这里 多斗争后https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

+0

感谢您的响应,通过法典读出并与此想出: '的$ args =阵列( \t 'post_type'=> '产品', \t 'posts_per_page'=> -1, \t“税查询” =>数组(阵列( \t \t \t '分类'=> 'product_cat', \t \t \t '字段'=> '蛞蝓', \t \t \t '术语'=>数组($ CAT- > slug), \t \t \t“操作” =“IN” \t \t \t)) \t \t)>;' 它没有做任何事情虽然还只是展示,而不是在PHP非常好所有的产品 - 我是在正确的轨道上? –

+0

是的,你正处于正确的轨道,你可以改变:'tax-query'=> array(array('taxonomy'=>'product_cat','field'=>'slug','terms'=> array($ cat - > slug),'operator'=>'IN'))数组以符合您的要求。例如,运算符支持“IN”,“=”,“LIKE”,“>”,“<”等。 –

+0

哦,我的话,只是意识到我正在使用'tax_query'而不是'tax_query' - 带下划线! 也就是说,如果我在数组($ category-> slug)的位置添加术语名称(例如“引擎”),它会起作用,如果我添加变量调用什么也不显示。我怀疑它可能会调用页面上的其他内容? –

相关问题