2015-11-02 85 views
1

背景: WooCommerce提供了一個簡碼,顯示最近的產品我想要的任何地方。WooCommerce - 最新產品膠印

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

有一個在WP_Query參數命名偏移,使我們能夠越過所需數量的職位。

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

所以,如果我用上面的查詢遍歷職位,第一個結果我會得到將是第四個最新的帖子。對?

問題:我想知道是否可以擴展WC的Recent Posts shortcode來接受偏移量參數?

回答

2

你必須在wp-content/plugins/woocommerce/includes/class-wc-shortcodes.php改變recent_products()方式類似:

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使用。

+0

這似乎是一個溫和的解決方案。我無法找到shortcodes-init.php文件。或者它是我必須創建的文件?不好意思打擾,但我對WooCommerce不太好。 –

+0

抱歉,這是舊版本。看看更新的答案。 – vard

+0

很酷。是否有可能在不更改插件本身的情況下執行?因爲更改會在下一次更新之後進行。對? –