首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >显示过去10天内添加的产品woocommerce

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

Stack Overflow用户
提问于 2021-03-20 02:04:54
回答 1查看 66关注 0票数 0

我正在尝试做一个woo商业页面,只显示过去10天的最新产品。下面的代码可以工作。

代码语言:javascript
复制
<?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"

我试着添加了

代码语言:javascript
复制
$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')) 
        )
        );

无济于事。我也试过了。

代码语言:javascript
复制
$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天。任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-20 18:16:52

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

代码语言:javascript
复制
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/

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66713491

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档