我想在我的页面模板上根据产品类别获取WooCommerce产品。
假设我有一个类别 mattress
。我想获取与 mattress
类别相关的所有产品。
这是我的代码:
<?php
// Start the Loop.
while ( have_posts() ) : the_post();
// Include the page content template.
get_template_part( 'content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) {
comments_template();
}
endwhile;
?>
我怎样才能做到这一点?
感谢。
答案 0 :(得分:2)
- - (8月8日更新2) - -
最后一个好的选择是使用has_term();
wordpress function来过滤 if
语句中某些类别的所有产品。如果您有多个类别,则可以使用 has_term();
功能将它们嵌入到数组中。
正如我之前在评论中所说,我认为你的问题在这里:
// Include the page content template. get_template_part( 'content', 'page' );
所以我会用一些自定义代码替换它,以向您显示条件正常:
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => -1
);
$loop = new WP_Query( $args );
// Start the Loop
If($loop->have_posts()){
while ( $loop->have_posts() ) : $loop->the_post();
// Here it is your product category
if ( has_term( 'mattress', 'product_cat', $loop->post ); ) {
// from here I display products thumbnails and name
echo '<div class="woocommerce-product" style="padding:5px; float:left;">';
if (has_post_thumbnail( $product_id )) {
echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog');
} else {
echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />';
}
echo '<div class="product-name">' . $loop->post->post_name . '</div>';
echo '</div>';
}
endwhile;
}
// If needed
wp_reset_query();
wp_reset_postdata();
?>
它也可以无条件地运行:has_term( 'mattress', 'product_cat', $loop->post );
用这个替换 $args
数组:
$args = array(
'post_type' => 'product',
'posts_per_page' => -1, // you can set here number of post per page
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'mattress '
) )
);
此代码经过测试且功能齐全 ,它会在您的活动子主题或主题的function.php文件中进行。
参考:
- - (更新1 - 7月9日) - -
或者您也可以使用has_category();
功能以这种方式过滤 if
语句:
<?php
global $post;
// Start the Loop
while ( have_posts() ) : the_post();
if ( has_category( 'mattress', $post ); ) {
// Include the page content template.
get_template_part( 'content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) {
comments_template();
}
}
endwhile;
?>
你可以试试这个:
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => -1, // you can set here number of post per page
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'mattress '
) )
);
$loop = new WP_Query($args);
If($loop->have_posts()){
while($loop->have_posts()) {
$loop->the_post();
// Include the page content template.
get_template_part( 'content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) {
comments_template();
}
}
}
// If needed
wp_reset_query();
wp_reset_postdata();
?>
答案 1 :(得分:1)
下面的代码应该可以使用。
<?php
$query = new WP_Query('posts_per_page=5&post_type=product&product_cat=mattress');
If($query->have_posts()){
while($query->have_posts()) {
$query->the_post();
// Your Code
}
}
wp_reset_query();
wp_reset_postdata();
?>
答案 2 :(得分:1)
<ul>
<?php
$taxonomy = 'product_cat';
$orderby = 'title';
$show_count = 0;
$pad_counts = 0;
$hierarchical = 1;
$title = '';
$empty = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$all_categories = get_categories( $args );
foreach ($all_categories as $cat) {
$category_id = $cat->term_id;?>
<li><a href="<?php echo get_term_link($cat->slug, 'product_cat');?>"><?php echo $cat->name;?></a></li>
<?php }?>
</ul>