【问题标题】:Place IF ELSE statement inside Woocommerce product loop - Wordpress在 Woocommerce 产品循环中放置 IF ELSE 语句 - Wordpress
【发布时间】: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


    【解决方案1】:

    试试这个例子,

    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 文件或任何插件文件中。

      现在应该对你有用了……

      【讨论】:

        猜你喜欢
        • 2015-10-28
        • 1970-01-01
        • 1970-01-01
        • 2017-02-17
        • 2015-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多