【发布时间】:2018-11-25 17:40:01
【问题描述】:
我正在尝试从所有产品中获取包含特定产品属性“颜色”的所有自定义术语的数组。
这是我的功能代码:
// custom_terms_taxonomy function
function custom_terms_taxonomy()
{
$custom_terms = array();
// array with products
$products_array = array( 'post_type' => 'product', 'posts_per_page' => -1 );
foreach ( $products_array as $product_array ) {
// attribute string
$terms_array = $product_array->get_attribute( 'pa_color' );
// array with attributes
$terms_array = ! empty($terms_array) ? (array) explode(', ', $terms_array) : array();
foreach ( $terms_array as $term_array ) {
// check if the value exist aleready in array
if (!in_array($term_array, $custom_terms)) {
// add attributes to Custom_array
array_push($custom_terms,$term_array);
}
}
}
return $custom_terms;
}
//output
$att_array=custom_terms_taxonomy();
print_r($att_array);
这个脚本不起作用。任何帮助表示赞赏。
【问题讨论】:
标签: php wordpress woocommerce custom-taxonomy taxonomy-terms