Woocommerce 在当前类别的所有产品之后显示其他类别
Woocommerce to show other category after all product in current one
是否有插件或代码可以在打开某个类别后显示所有产品以显示其他类别的产品。因为某些类别或 sub-category 只有 2-4 种产品,我想用其他类似类别的产品填充页面。
示例:手套
手套页面标题
2-4 个产品
然后是某个类别的靴子,有 5-6 个产品
谢谢!
执行此操作的简单方法可能只是创建多个类别 - 例如 "apparel" 类别并将手套 和 靴子分配给它。更复杂的方法可能是使用 WP_Query 生成列表。
$args = array(
'post_type' => 'product',
'posts_per_page' => 9,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'my-glove-category'
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'my-boots-category'
)
)
);
$executedQuery = new WP_Query($args);
if ($executedQuery->have_posts()) {
while ($executedQuery->have_posts()) {
$executedQuery->the_post(); //increment the post pointer, getting us the next post in the list
echo '<h2>' . get_the_title() . '</h2>';
}
}
else {
echo 'No products were found.';
}
此示例将抓取 my-glove-category
或 my-boots-category
中的每个产品。如果你想按类别排序,那就开始变得有点困难了。
您还可以使用 product_tag
作为这些查询的分类法,请参阅 the installed taxonomies and post types for WooCommerce.
是否有插件或代码可以在打开某个类别后显示所有产品以显示其他类别的产品。因为某些类别或 sub-category 只有 2-4 种产品,我想用其他类似类别的产品填充页面。
示例:手套
手套页面标题
2-4 个产品
然后是某个类别的靴子,有 5-6 个产品
谢谢!
执行此操作的简单方法可能只是创建多个类别 - 例如 "apparel" 类别并将手套 和 靴子分配给它。更复杂的方法可能是使用 WP_Query 生成列表。
$args = array(
'post_type' => 'product',
'posts_per_page' => 9,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'my-glove-category'
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'my-boots-category'
)
)
);
$executedQuery = new WP_Query($args);
if ($executedQuery->have_posts()) {
while ($executedQuery->have_posts()) {
$executedQuery->the_post(); //increment the post pointer, getting us the next post in the list
echo '<h2>' . get_the_title() . '</h2>';
}
}
else {
echo 'No products were found.';
}
此示例将抓取 my-glove-category
或 my-boots-category
中的每个产品。如果你想按类别排序,那就开始变得有点困难了。
您还可以使用 product_tag
作为这些查询的分类法,请参阅 the installed taxonomies and post types for WooCommerce.