我有一个短代码,它为post category slug和returns以及post列表接受1个参数。
//[list-products] creates the product table in the wholesale page, can be used to create a table list of any category
function list_products_func ( $atts ) {
$a = shortcode_atts( array (
\'category\' => \'\',
), $atts);
$args = array(
\'posts_per_page\' => -1,
\'tax_query\' => array(
\'relation\' => \'AND\',
array(
\'taxonomy\' => \'product_cat\',
\'field\' => \'slug\',
\'terms\' => $a[\'category\']
)
),
\'post_type\' => \'product\',
\'orderby\' => \'title,\'
);
$products = new WP_Query( $args );
echo "<table>";
while ( $products->have_posts() ) {
$products->the_post();
$product = new WC_Product(get_the_ID());
?>
<tr class="product-list-item">
<td><a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a></td>
<td>
<?php echo get_the_excerpt(); ?>
</td>
<td>
<?php echo $product->get_price_html(); ?>
</td>
<td>
<?php echo woocommerce_quantity_input(
array(
\'min_value\' => 1,
\'max_value\' => $product->backorders_allowed() ? \'\' : $product->get_stock_quantity(),
)); ?>
</td>
<td>
<?php echo woocommerce_template_loop_add_to_cart(); ?>
</td>
</tr>
<?php
}
echo "</table>";
}
add_shortcode ( \'list_products\' , \'list_products_func\' );
我希望它能够采取任何数量的类别,并返回符合这两个类别的帖子。在本用例中,咖啡经销商使用它来展示WooCommerce产品,这些产品的类别为
wholesale
和他们的咖啡子类别之一
distinct-well-defined
或者别的什么。
它的使用方式是[list-products CAT1 CAT2]
我试着用逗号键入短代码,希望代码能正确解释,但没有成功。
任何帮助都将不胜感激。
事实证明,我实际上需要它来过滤产品,所以它只显示两个类别的产品,而不显示两个类别中的所有产品。
最合适的回答,由SO网友:Robbert 整理而成
也许您可以这样做,以便使用像[列出产品类别=“1,2,3”]这样的短代码,然后将它们分解为一个数组,插入它们作为您的术语?
<?php
function list_products_func($atts) {
$a = shortcode_atts(array(
\'categories\' => \'\',
), $atts);
// Check for multiple categories by comma
if ( strpos( $a[\'categories\'], \',\' ) !== false ) {
$terms = explode( $a[\'categories\'] );
} else {
$terms = $a[\'categories\'];
}
$args = array(
\'posts_per_page\' => -1,
\'tax_query\' => array(
\'relation\' => \'AND\',
array(
\'taxonomy\' => \'product_cat\',
\'field\' => \'slug\',
\'terms\' => $terms // Your exploded terms array
)
),
\'post_type\' => \'product\',
\'orderby\' => \'title,\'
);
$products = new WP_Query($args);
echo "<table>";
while ($products->have_posts()) {
$products->the_post();
$product = new WC_Product(get_the_ID()); ?>
<tr class="product-list-item">
<td><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></td>
<td><?php echo get_the_excerpt(); ?></td>
<td><?php echo $product->get_price_html(); ?></td>
<td>
<?php echo woocommerce_quantity_input(
array(
\'min_value\' => 1,
\'max_value\' => $product->backorders_allowed() ? \'\' : $product->get_stock_quantity(),
)); ?>
</td>
<td><?php echo woocommerce_template_loop_add_to_cart(); ?></td>
</tr>
<?php
}
echo "</table>";
}
add_shortcode(\'list_products\', \'list_products_func\');
?>
SO网友:Khaled Allen
这是对我问题第二部分的回答,直到得到公认的答案后,我才意识到这一点。
首先,我添加了Robbert的公认答案和这个小for
循环以展开tax_query
动态参数:
//Generates an array of tax_query arguments
$query_args = array ();
foreach ( $terms as $term ) {
array_push( $query_args, array (
\'taxonomy\' => \'product_cat\',
\'field\' => \'slug\',
\'terms\' => $term
)
);
}
$args = array(
\'posts_per_page\' => -1,
\'tax_query\' => array(
\'relation\' => \'AND\',
$query_args
),
\'post_type\' => \'product\',
\'orderby\' => \'title,\'
);