在 Woocommerce 产品循环中放置 IF ELSE 语句 - Wordpress

Place IF ELSE statement inside Woocommerce product loop - Wordpress

我有以下自定义短代码来显示一系列产品

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

我正在尝试在循环内添加一条文本消息,如果没有要显示的产品,它将显示 "No products to display"。

我正在努力正确放置语句而不出现语法错误。

我一直在摆弄这样的代码:

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

但这是不正确的。

你能给我指明正确的方向吗?

试试这个例子,

function newsItems( $atts ) {
$news_query= null;
$args = array(
  'post_type'      => 'news',
  'post_status'    => 'publish',
  'posts_per_page' => 10,
);

$news_query = new WP_Query( $args );
$output = '';
if ( $news_query->have_posts() ) {
  $output .= '<div class="news-shortcode-posts">';
  while ( $news_query->have_posts() ) : $news_query->the_post();
      ob_start();
      get_template_part( 'inc/news-item' );
      $output .= ob_get_clean();
  endwhile;
  if( $show_archive == 'true' ) {
      $output .= '<div class="full-width align-right">';
      $output .= 'See All Archives';
      $output .= '</div>';
  }
  $output .= '</div>';
  }
  return $output;
}
add_shortcode('teamsters-news', 'newsItems');

希望这对您有所帮助。想要查询更多的信息。

2 件事:

1) 您已经删除了 while 循环
2) 此行有一个错误:

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

应该改为 (在您的代码中):

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

因此,以下代码应该是实现此功能的正确方法:

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

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。

这应该对你有用了……