duanba7498 2015-11-02 14:01
浏览 12

WooCommerce - 最近的产品抵消

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

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

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?

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

  • 写回答

1条回答 默认 最新

  • dongxiaowei_1234 2015-11-02 14:13
    关注

    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' );
        }
    

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

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀