显示仅在过去 10 天 woocommerce 中添加的产品

Posted

技术标签:

【中文标题】显示仅在过去 10 天 woocommerce 中添加的产品【英文标题】:Showing products only added in the last 10-days woocommerce 【发布时间】:2021-03-19 18:04:54 【问题描述】:

我正在尝试制作一个仅显示过去 10 天的最新产品的 woo 商务页面。下面的代码有效。

<?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 种产品。 (不太可能)没有可用的分页。我想这取决于它在页面上的事实? file name: "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 天。任何帮助将不胜感激。

【问题讨论】:

您可以在代码中使用woo_pagination() 函数。否则,我认为有人问过类似的问题here。 【参考方案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/

【讨论】:

以上是关于显示仅在过去 10 天 woocommerce 中添加的产品的主要内容,如果未能解决你的问题,请参考以下文章

仅在 WooCommerce 中获取当前产品的产品标签

仅在所有 WooCommerce 单品上显示价格后缀

仅在特定产品变体属性中显示Woocommerce购物车中的消息

woocommerce-如何仅在商店页面上显示子类别?

仅在可见时才需要 Woocommerce 结帐字段

仅在 Woocommerce 结帐页面中将文本附加到总价