【发布时间】:2017-12-29 19:29:52
【问题描述】:
我必须制作一个 Wordpress 插件,为 WooCommerce 添加短代码。我想从特定产品类别中获取产品以及要显示的最大产品数量。短代码参数应该是类别 ID 和产品限制。我想我应该使用WP_Query 对象。
我需要让它看起来像这样:
短代码如下:[productslist_category="[category_ID]" limit="[product_limit]"]
我使用了from this answer下面的代码(感谢LoicTheAztec):
if( !function_exists('products_list_in_a_product_category') ) {
function products_list_in_a_product_category( $atts ) {
// Shortcode Attributes
$atts = shortcode_atts(
array(
'cat' => '',
'limit' => '4', // default product per page
'column' => '4', // default columns
),
$atts, 'productslist'
);
// The query
$posts = get_posts( array(
'post_type' => 'product',
'posts_per_page' => intval($atts['limit'])+1,
'product_cat' => $atts['cat'],
) );
$output = '<div class="products-in-'.$atts['cat'].'">';
// The loop
foreach($posts as $post_obj)
$ids_array[] = $post_obj->ID;
$ids = implode( ',', $ids_array );
$columns = $atts['column'];
$output .= do_shortcode ( "[products ids=$ids columns=$columns ]" ) . '</div>';
return $output;
}
add_shortcode( 'productslist', 'products_list_in_a_product_category' );}
但是我得到一个错误。它说 implode 函数有问题。
【问题讨论】:
-
我说内爆函数有错误
-
警告:implode():传递的参数无效
-
没有警告也没有错误……请在下面的答案中查看我的 2 个经过测试的实时链接。该错误是由于您的安装中的其他原因造成的。您需要将 PHP 5.6 与 WP 和 WooCommerce 一起使用……这段代码完美运行
标签: php wordpress plugins woocommerce shortcode