2017-12-05 2 views
0

Ich habe die folgenden benutzerdefinierten Short bekommt eine Reihe von Produkten anzuzeigenPlatz IF ELSE-Anweisung innerhalb WooCommerce Produkt Schleife - 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>'; 
} 

Ich versuche, eine SMS-Nachricht innerhalb der Schleife hinzufügen, die „sagen: Nein Produkte zum Anzeigen "wenn keine Produkte angezeigt werden.

Ich habe Schwierigkeiten, die Anweisung korrekt zu platzieren, ohne einen Syntaxfehler zu bekommen.

Ich habe mit dem Code wie folgt Hantieren:

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

Aber das ist nicht richtig.

Können Sie mir bitte in die richtige Richtung zeigen?

Antwort

1

2 Dinge:

1) Sie haben die while-Schleife entfernt.
2) Es gibt einen Fehler in dieser Zeile:

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

Es sollte stattdessen sein (im Code):

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

So ist der folgende Code sollte sein Der richtige Weg, um das zu erreichen:

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 geht in function.php Datei von Ihrem aktiven Kind Thema (oder Thema) oder auch in einer beliebigen Plugin-Datei.

...

+1

Legende Dies sollte nun für Sie arbeiten! Das funktioniert super, vielen Dank. –

0

dieses Beispiel,

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

hoffen, dass dies Ihnen hilft. für mehr Informationen.

Verwandte Themen