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 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据