提问者:小点点

仅显示过去10天内添加的产品


我正在尝试制作一个只显示最近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
        $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(); ?>

但是,如果在过去10天内添加了超过100个产品。(不太可能)没有可用的分页。我想这是因为它在一页上?文件名:page-new-arrivals.php

我试图补充一点

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$args = array(
        'post_type' => 'product',
        'posts_per_page' => 100,
        'order' => 'DESC',
        'paged'=>$paged,
        'date_query' => array(
            'after' => date('Y-m-d', strtotime('-10 days')) 
        )
        );

无济于事。我也试过这个。

$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天。任何帮助都将不胜感激。


共1个答案

匿名用户

因此,我决定从另一个角度来看这一点,并钩住已经提供的短代码,我知道分页是有效的。

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"]

以上代码完美工作!呜。偶然发现这个话题,刚刚劫持了我的日期查询。

https://wordpress.org/support/topic/adding-a-special-product-attribute-to-the-products-shortcode/