doujuegai8830 2017-12-05 08:56
浏览 64
已采纳

将IF ELSE声明置于Woocommerce产品循环中 - Wordpress

I've got the following custom shortcode to display a range of products

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>';
}

I am trying to add a text message inside the loop that will say "No products to display" if there no products to show.

I'm struggling to correctly place the statement without getting a syntax error.

I've been fiddling around with the code like this:

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>';
}

But that's not correct.

Could you please point me to the right direction?

  • 写回答

2条回答 默认 最新

  • dongyan0629 2017-12-05 18:49
    关注

    2 things:

    1) You have removed the while loop.
    2) There is an error in this line:

    if ( $products->have_posts() ) : $products->the_post() {
    

    It should be instead (in your code):

    if ( $products->have_posts() ) { 
        $products->the_post();
    

    So the following code, should be the correct way to get this working:

    add_shortcode( 'my_shortcode_name', 'on_sale_products' );
    function on_sale_products() {
        global $product, $woocommerce, $woocommerce_loop;
    
        $products = new WP_Query( apply_filters('woocommerce_related_products_args', array(
           // this is working array, just empty for this example
        ) ) );
    
        ob_start();
        woocommerce_product_loop_start();
    
        if ( $products->have_posts() ):
            while ( $products->have_posts() ): 
               $products->the_post();
               wc_get_template_part( 'content', 'product' ); 
            endwhile;
        else:
            echo '<div class="no-products">There are no products to display</div>';
        endif; 
    
        woocommerce_product_loop_end();
        woocommerce_reset_loop();
        wp_reset_postdata();
    
        return '<div class="on-sale">' . ob_get_clean() . '</div>';
    }
    

    Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    This should work for you now…

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function