【发布时间】:2018-05-18 20:59:03
【问题描述】:
我有以下自定义短代码来显示一系列产品
add_shortcode( 'my_shortcode_name', 'on_sale_products' );
function on_sale_products() {
global $product, $woocommerce, $woocommerce_loop;
$args = apply_filters('woocommerce_related_products_args', array(
// this is working array, just empty for this example
)
);
$products = new WP_Query( $args );
ob_start();
woocommerce_product_loop_start();
while ( $products->have_posts() ) : $products->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
woocommerce_product_loop_end();
woocommerce_reset_loop();
wp_reset_postdata();
return '<div class="on-sale">' . ob_get_clean() . '</div>';
}
我正在尝试在循环中添加一条文本消息,如果没有要显示的产品,它将显示“没有要显示的产品”。
我正在努力正确放置语句而不会出现语法错误。
我一直在摆弄这样的代码:
add_shortcode( 'my_shortcode_name', 'on_sale_products' );
function on_sale_products() {
global $product, $woocommerce, $woocommerce_loop;
$args = apply_filters('woocommerce_related_products_args', array(
// this is working array, just empty for this example
)
);
$products = new WP_Query( $args );
ob_start();
woocommerce_product_loop_start();
if ( $products->have_posts() ) : $products->the_post() {
wc_get_template_part( 'content', 'product' );
} else {
echo '<div class="no-products">There are no products to display</div>';
}
woocommerce_product_loop_end();
woocommerce_reset_loop();
wp_reset_postdata();
return '<div class="on-sale">' . ob_get_clean() . '</div>';
}
但这是不正确的。
能否请您指出正确的方向?
【问题讨论】:
标签: php wordpress woocommerce arguments hook-woocommerce