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 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…
Try this example,
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');
Hope this will helps you. for more information.