Php 将IF-ELSE语句放入Woocommerce产品循环中-Wordpress
我有以下自定义短代码来显示一系列产品Php 将IF-ELSE语句放入Woocommerce产品循环中-Wordpress,php,wordpress,woocommerce,arguments,hook-woocommerce,Php,Wordpress,Woocommerce,Arguments,Hook Woocommerce,我有以下自定义短代码来显示一系列产品 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
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');
销售产品的职能(){
全球$product、$WOOCOMERCE、$WOOCOMERCE\u loop;
$args=apply_filters('woocommerce_相关产品_args',数组(
//这是工作数组,在本例中为空
)
);
$products=新的WP_查询($args);
ob_start();
woocommerce_product_loop_start();
而($products->have_posts()):$products->the_post();
wc_获取_模板_零件(‘内容’、‘产品’);
结束时;
woocommerce_product_loop_end();
woocommerce_reset_loop();
wp_reset_postdata();
返回“”ob_get_clean()。';
}
我试图在循环中添加一条文本消息,如果没有要显示的产品,该消息将显示“没有要显示的产品”
我正在努力正确地放置语句而不会出现语法错误
我一直在摆弄这样的代码:
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>';
}
add_shortcode('my_shortcode_name'、'on_sale_products');
销售产品的职能(){
全球$product、$WOOCOMERCE、$WOOCOMERCE\u loop;
$args=apply_filters('woocommerce_相关产品_args',数组(
//这是工作数组,在本例中为空
)
);
$products=新的WP_查询($args);
ob_start();
woocommerce_product_loop_start();
如果($products->have_posts()):$products->the_post(){
wc_获取_模板_零件(‘内容’、‘产品’);
}否则{
echo“没有可展示的产品”;
}
woocommerce_product_loop_end();
woocommerce_reset_loop();
wp_reset_postdata();
返回“”ob_get_clean()。';
}
但这是不对的
你能给我指一下正确的方向吗?试试这个例子
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');
功能新闻项($atts){
$news\u query=null;
$args=数组(
“post_type”=>“news”,
“发布状态”=>“发布”,
“每页帖子数”=>10,
);
$news\u query=新的WP\u查询($args);
$output='';
如果($news\u query->have\u posts()){
$output.='';
而($news\u query->have\u posts()):$news\u query->the\u post();
ob_start();
获取模板部分(“inc/news item”);
$output.=ob_get_clean();
结束时;
如果($show_archive==“true”){
$output.='';
$output.='查看所有存档';
$output.='';
}
$output.='';
}
返回$output;
}
添加快捷码(“卡车司机新闻”、“新闻项目”);
希望这对你有帮助。了解更多信息
- 两件事:
1) 您已删除while循环
2) 这一行中有一个错误:
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>';
}
add_shortcode('my_shortcode_name'、'on_sale_products');
销售产品的职能(){
全球$product、$WOOCOMERCE、$WOOCOMERCE\u loop;
$products=新的WP_查询(应用_筛选器('WOOMerce_相关_产品_参数',数组(
//这是工作数组,在本例中为空
) ) );
ob_start();
woocommerce_product_loop_start();
如果($products->have_posts()):
而($products->have_posts()):
$products->the_post();
wc_获取_模板_零件(‘内容’、‘产品’);
结束时;
其他:
echo“没有可展示的产品”;
endif;
woocommerce_product_loop_end();
woocommerce_reset_loop();
wp_reset_postdata();
返回“”ob_get_clean()。';
}
代码位于活动子主题(或主题)的function.php文件或任何插件文件中
这应该对你有用了
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>';
}