WooCommerce - 最近的产品抵消 [英] WooCommerce - Recent Products Offset

查看:21
本文介绍了WooCommerce - 最近的产品抵消的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景: WooCommerce 提供了一个短代码,可以在我想要的任何地方显示最近的产品.

Background: WooCommerce provides a shortcode to display recent products any place I want.

  <?php echo do_shortcode('[recent_products columns="3"]'); ?>

WP_Query 中有一个名为 offset 的参数,它允许我们传递所需数量的帖子.

There is an argument in WP_Query named offset that allows us to pass over desired number posts.

  <?php $query = new WP_Query( array( 'offset' => 3 ) ); ?>

因此,如果我使用上述查询来遍历帖子,我得到的第一个结果将是第四个最新帖子.对吗?

So, if I use the above query to loop over posts, the first result I'd get would be the fourth latest post. Right?

问题:我想知道是否可以扩展 WC 的近期帖子"短代码以接受偏移量参数?

Question: I was wondering if it would be possible to extend WC's Recent Posts shortcode to accept offset argument?

推荐答案

您必须在 wp-content/plugins/woocommerce/includes/class-wc-shortcodes.php 中更改 recent_products() 像这样的方法:

You'll have to change in wp-content/plugins/woocommerce/includes/class-wc-shortcodes.php the recent_products() method like that:

public static function recent_products( $atts ) {
        $atts = shortcode_atts( array(
            'per_page' => '12',
            'columns'  => '4',
            'orderby'  => 'date',
            'order'    => 'desc',
            'offset'   => 0,
            'category' => '',  // Slugs
            'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
        ), $atts );
        $query_args = array(
            'post_type'           => 'product',
            'post_status'         => 'publish',
            'ignore_sticky_posts' => 1,
            'posts_per_page'      => $atts['per_page'],
            'orderby'             => $atts['orderby'],
            'order'               => $atts['order'],
            'offset'              => $atts['offset'],
            'meta_query'          => WC()->query->get_meta_query()
        );
        $query_args = self::_maybe_add_category_args( $query_args, $atts['category'], $atts['operator'] );
        return self::product_loop( $query_args, $atts, 'recent_products' );
    }

通过此添加一个 offset 属性(默认为 0),将在 WP_Query 中使用.

With this an offset attribute is added (default 0) that will be used in WP_Query.

这篇关于WooCommerce - 最近的产品抵消的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆