使用自定义元查询显示带有短代码的 WooCommerce 产品
我正在创建一个闪购网站,并且已经在我的主页和商店页面上根据日期范围展示了产品.但我也想根据其他地方的日期范围显示产品,因此使用简码.
I'm creating a flash sale website and I already display products according to date range on my home and shop pages. But I also want to display products according to a date range elsewhere and therefore using a shortcode.
这是我的代码:
function testt($meta_query)
{
$today = current_time('Ymd');
$args = apply_filters('woocommerce_shortcode_products_query', array (
'post_type' => 'product',
'numberposts' => -1,
'meta_query' => array(
'relation' => 'AND',
'start_clause' => array(
'key'=>'flash_sale_start',
'value' => $today,
'compare'=> '<=',
'type' => 'DATE'
),
'end_clause' => array(
'key' => 'flash_sale_end',
'value' => $today,
'compare' => '>=',
'type' => 'DATE'
),
)));
return $args;
}
add_shortcode( 'test', 'testt' );
但它没有显示任何内容,甚至我页面的其余内容都消失了.
But it doesn't display anything, even the rest of my page content has disappeared.
我做错了什么?
感谢任何帮助.
这是正常的,它不返回任何内容,因为您需要在 WP_Query
中传递此 $args
代码>首先以这种方式循环调用产品模板:
This is normal that it doesn't return anything as you need to pass this $args
in a WP_Query
first and to call a the product template in a loop this way:
if( ! function_exists('product_test') ) {
// Add Shortcode
function product_test( $atts ) {
global $woocommerce_loop;
// Attributes
$atts = shortcode_atts(
array(
'columns' => '4',
'limit' => '20',
'start' => current_time('Ymd'),
'end' => current_time('Ymd'),
),
$atts, 'products_test'
);
$woocommerce_loop['columns'] = $atts['columns'];
// The WP_Query
$products = new WP_Query( array (
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => $atts['limit'],
'meta_query' => array(
'relation' => 'AND',
'start_clause' => array(
'key' =>'flash_sale_start',
'value' => $atts['today'],
'compare' => '<=',
'type' => 'DATE'
),
'end_clause' => array(
'key' => 'flash_sale_end',
'value' => $atts['today'],
'compare' => '>=',
'type' => 'DATE'
),
)
));
ob_start();
if ( $products->have_posts() ) { ?>
<?php woocommerce_product_loop_start(); ?>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
<?php
} else {
do_action( "woocommerce_shortcode_products_loop_no_results", $atts );
echo "<p>There is no results.</p>";
}
woocommerce_reset_loop();
wp_reset_postdata();
return '<div class="woocommerce columns-' . $atts['columns'] . '">' . ob_get_clean() . '</div>';
}
add_shortcode( 'products_test', 'product_test' );
}
代码位于活动子主题(或活动主题)的 function.php 文件中.
用法:
您可以将 4 个可用的可选参数添加到此短代码中:
There is 4 available optional arguments that you can add to this shortcode:
-
columns
(列数)- 默认为 4 -
limit
(产品数量| -1 将显示所有) - 默认为 20 -
start
(开始日期 | 格式为 'YMD' )- 默认为今天 -
end
(结束日期 | 格式为 'YMD' )- 默认为今天
-
columns
(The number of columns) - Default is 4 -
limit
(the number of products | -1 will display all ) - Default is 20 -
start
(Start date | format is 'YMD' ) - Default is today -
end
(End date | format is 'YMD' ) - Default is today
您可以在函数中设置更多……您也可以更改默认值.
You can set any more in the function… You can change default values too.
示例 1(简单的默认值):
Example 1 (simple with default values):
[products_test]
示例 2(一些自定义值)
Example 2 (some custom values)
[products_test columns='3' limit='15']
经过测试并有效