如何在WordPress中按类别显示相关产品?
产品类别:
例如,当前页面是“高架地板”,仅显示辅助产品,可耐福地板,相关产品中的再生板。
我在single-products.php中添加了代码,如下所示:
$orderby = 'name';
$order = 'random';
$hide_empty = true ;
$cat_args = array(
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
);
$product_categories = get_terms( 'product_category', $cat_args );
if( !empty($product_categories) ){
echo '<div class="container">';
echo '<div class="row">';
foreach ($product_categories as $key => $category) {
$description = $category->description;
echo '<div class="col-lg-6">';
echo '<div class="card">';
echo '<a href="'.get_term_link($category).'" >';
$image = get_field('product_category', $category );
if($image) {
echo '<img class="card-img-top" src="' . $image['url'] . '" alt="' . $image['alt'] .'">';
} else {
echo '<img class="card-img-top" src="/wp-content/uploads/2018/07/placeholder.png">';
}
echo '<div class="card-content">';
echo '<h3>' . $category->name . '</h3>';
echo '<p>';
echo strlen($description) > 60 ? substr($description, 0, 100) . '...' : $description;
echo '</p>';
echo '<button class="button btn_medium btn_orange btn_squared btn_normal_style" href="'.get_term_link($category).'" >Discover more</button>';
echo '</div>';
echo '</a>';
echo '</div>';
echo '</div>';
}
echo '</div>';
echo '</div>';
}
else {
// no posts found
echo wpautop( 'Sorry, no products were found' );
}
答案 0 :(得分:0)
首先,获取与当前产品关联的类别的条款:
$terms = wp_get_post_terms( get_the_id(), 'product_cat', array('fields' => 'ids') );
当前产品的ID是通过以下函数获取的:get_the_id()
第二,获得其他具有相同条款的产品(结果中不包括当前产品):
$products = get_posts(array(
'post_type' => 'product',
'post_status' => 'publish',
'numberposts' => -1,
'post__not_in' => array(get_the_id()),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $terms,
'include_children' => false
)
)
));
完整的代码是:
$terms = wp_get_post_terms( get_the_id(), 'product_cat', array('fields' => 'ids') );
$products = get_posts(array(
'post_type' => 'product',
'post_status' => 'publish',
'numberposts' => -1,
'post__not_in' => array(get_the_id()),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $terms,
'include_children' => false
)
)
));
答案 1 :(得分:0)
@MikeD这仅显示当前产品,而不显示其他3个产品,我做错了吗?
`<?php
$orderby = 'name';
$order = 'random';
$hide_empty = true ;
$cat_args = array(
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
);
$terms = wp_get_post_terms( get_the_id(), 'product_category', array('fields' => 'ids') );
echo '<div class="container">';
echo '<div class="row">';
foreach ($terms as $key => $term) {
$term = get_term($term_id);
echo '<a href="' .get_term_link($term). '" ></a>';
echo '<div class="col-lg-6">';
echo '<div class="card">';
echo '<a href="'.get_term_link($term).'" >';
$image = get_field('product_category', $term );
if($image) {
echo '<img class="card-img-top" src="' . $image['url'] . '" alt="' . $image['alt'] .'">';
} else {
echo '<img class="card-img-top" src="/wp-content/uploads/2018/07/placeholder.png">';
}
echo '<div class="card-content">';
echo '<h3>' . $term->name . '</h3>';
//echo $category->name;
//echo '<p>' . $category->description . '</p>';
echo '<p>';
echo strlen($description) > 60 ? substr($description, 0, 100) . '...' : $description;
echo '</p>';
//echo '<p>' . mb_strimwidth($category->description, 0, 30, "...") . '</p>';
echo '<button class="button btn_medium btn_orange btn_squared btn_normal_style" href="'.get_term_link($term).'" >Discover more</button>';
echo '</div>';
echo '</a>';
echo '</div>';
echo '</div>';
}
echo '</div>';
echo '</div>';
?>`
答案 2 :(得分:0)
看看我得到的数组中只有条件的ID:
$terms = wp_get_post_terms( get_the_id(), 'product_cat', array('fields' => 'ids') );
因此,您应该编辑代码段:
foreach ($terms as $key => $term) {
如下:
foreach ($terms as $key => $term_id) {
$term = get_term($term_id);
在您的代码段中,您没有将$ products变量与相关产品数组一起使用,这是您最初的问题。