Php 仅显示过去10天内添加的产品
我正在尝试制作一个只显示最近10天最新产品的woo commerce页面。下面的代码有效Php 仅显示过去10天内添加的产品,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我正在尝试制作一个只显示最近10天最新产品的woo commerce页面。下面的代码有效 <?php get_template_part('partials/page-title'); ?> <div class="row padded full-page" style="margin-top: 50px;"> <ul class="products columns-4"> <?php
<?php get_template_part('partials/page-title'); ?>
<div class="row padded full-page" style="margin-top: 50px;">
<ul class="products columns-4">
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 100,
'order' => 'DESC',
'date_query' => array(
'after' => date('Y-m-d', strtotime('-10 days'))
)
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</ul>
</div>
<?php get_footer(); ?>
无济于事。我也试过这个
$GLOBALS['wp_query']->max_num_pages = $loop->max_num_pages;
the_posts_pagination( array(
'mid_size' => 1,
'prev_text' => __( 'Back', 'green' ),
'next_text' => __( 'Onward', 'green' ),
'screen_reader_text' => __( 'Posts navigation' )
) );
我不知道!我有点迷路了。我到底需要什么?是否有办法通过函数注入woocommerce循环,以限制显示的产品,使其不超过30天。任何帮助都将不胜感激。因此,我决定从另一个角度来看待这一点,并将已经提供的短代码与之挂钩,我知道分页是有效的
add_filter('shortcode_atts_products', 'htdat_shortcode_atts_products', 10, 4);
function htdat_shortcode_atts_products( $out, $pairs, $atts, $shortcode ){
if ( isset ($atts[ 'daysold' ]) && $atts [ 'daysold' ] ) {
$out[ 'daysold' ] = true;
} else {
$out[ 'daysold' ] = false;
}
return $out;
}
add_filter( 'woocommerce_shortcode_products_query', 'htdat_woocommerce_shortcode_products_query', 10, 2 );
function htdat_woocommerce_shortcode_products_query( $query_args, $attributes ) {
if ( $attributes[ 'daysold' ] ) {
$query_args[ 'date_query' ] = array ('after' => date('Y-m-d', strtotime('-30 days')));
}
return $query_args;
}
[products daysold="true" limit="12" columns="4" paginate="true" orderby="date"]
上面的代码工作完美!呜呜。偶然发现这个主题,并劫持了我的日期查询
您可以在代码中使用该函数。否则,我想会有人问类似的问题。
add_filter('shortcode_atts_products', 'htdat_shortcode_atts_products', 10, 4);
function htdat_shortcode_atts_products( $out, $pairs, $atts, $shortcode ){
if ( isset ($atts[ 'daysold' ]) && $atts [ 'daysold' ] ) {
$out[ 'daysold' ] = true;
} else {
$out[ 'daysold' ] = false;
}
return $out;
}
add_filter( 'woocommerce_shortcode_products_query', 'htdat_woocommerce_shortcode_products_query', 10, 2 );
function htdat_woocommerce_shortcode_products_query( $query_args, $attributes ) {
if ( $attributes[ 'daysold' ] ) {
$query_args[ 'date_query' ] = array ('after' => date('Y-m-d', strtotime('-30 days')));
}
return $query_args;
}
[products daysold="true" limit="12" columns="4" paginate="true" orderby="date"]