我想按当前产品的menu_order对get_the_terms进行排序,直到现在我都拥有以下代码:
$colors='';
$terms = get_the_terms( get_the_ID(), 'pa_colors' );
foreach ( $terms as $term ) {
$colors='<pre>'.$term->name.'</pre>';
}
echo $colors;
有两种方法可以获取按菜单顺序排序的产品属性术语名称(对于已定义的产品):
1)。使用wp_get_post_terms()
功能 (WordPress方式)
WordPress功能get_the_terms()
不允许更改WP_Term_Query
…
因此,您将使用wp_get_post_terms()
允许WP_Term_Query
调整的类似内容。
$taxonomy = 'pa_color'; // The taxonomy
$query_args = array(
'fields' => 'names',
'orderby' => 'meta_value_num',
'meta_query' => array( array(
'key' => 'order_' . $taxonomy,
'type' => 'NUMERIC'
) )
);
$term_names = wp_get_post_terms( get_the_ID(), $taxonomy, $query_args );
if ( ! empty( $term_names ) ) {
// Output
echo '<pre>' . implode( '</pre><pre>', $term_names ) . '</pre>';
}
2)。简单使用WC_Product
方法get_attribute()
(WooCommerce方式)
$product = wc_get_product( get_the_ID() ); // The WC_product Object
$taxonomy = 'pa_color'; // The taxonomy
$names_string = $product->get_attribute('color');
if ( ! empty( $names_string ) ) {
$names_array = explode( ', ', $names_string ); // Converting the string to an array of term names
// Output
echo '<pre>' . implode( '</pre><pre>', $names_array ) . '</pre>';
}
两种方式都可以。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句