我正在尝试创建一个自定义的WooCommerce短码,我想从一个特定的目录在一个帖子的结尾显示特色产品。
有一个标准的短码:
[featured_products per_page="12" columns="4" orderby="date" order="desc"]我想要在其中添加catagory,因此新的短码将是:
[featured_category_products category="13" per_page="4" columns="4" orderby="date" order="desc"]要让它正常工作,就必须为它创建一个函数,所以我找到了包含所有默认快捷代码的class-wc-shortcodes.php文件。
我基于默认的特色产品添加了一个新功能:
public function featured_category_products( $atts ) {
global $woocommerce_loop;
extract(shortcode_atts(array(
'category' => '',
'per_page' => '4',
'columns' => '4',
'orderby' => 'date',
'order' => 'desc'
), $atts));
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => $per_page,
'orderby' => $orderby,
'order' => $order,
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array('catalog', 'visible'),
'compare' => 'IN'
),
array(
'key' => '_featured',
'value' => 'yes'
)
),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => array( esc_attr($category) ),
'field' => 'slug',
'operator' => 'IN'
)
)
);
ob_start();
$products = new WP_Query( $args );
$woocommerce_loop['columns'] = $columns;
if ( $products->have_posts() ) : ?>
<?php woocommerce_product_loop_start(); ?>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php woocommerce_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
<?php endif;
wp_reset_postdata();
return '<div class="woocommerce">' . ob_get_clean() . '</div>';
}我添加了类别变量extraction (并检查它是否有效),并添加了tax-query部分(从另一个根据类别显示产品的函数中找到)。所以我认为这应该行得通,但当然不行。我没有得到任何结果,也没有错误。
有谁知道这是怎么回事吗?
发布于 2013-11-12 22:04:37
如果您只寻找一个产品类别,请使用'terms' => $category。或者,您可以使用explode()将逗号分隔的字符串拆分为数组。
参见Wp Query : Taxonomy Parameters。
其他注释:
不要接触插件文件,您所做的更改将在下一次更新中丢失。
为您创建到it.
add_shortcode( 'featured_category_product','featured_category_product_function‘);
https://stackoverflow.com/questions/19930779
复制相似问题