【发布时间】:2021-06-16 14:10:10
【问题描述】:
我已经为“产品”帖子类型注册了两个分类法。叫他们pa_brand & pa_model
我创建了一个前端表单来编辑产品,这些产品通过$product_id 从 URL 参数中获取产品数据。
我需要的是通过$product_id获取pa_brand和pa_model的相关条款。
以下作品仅适用于单个产品页面,以便我可以获得与产品相关的正确条款。
$models = get_the_terms( $product->get_ID(), 'pa_model' );
echo '<pre>'; print_r( $models ); echo '</pre>';
$brands = get_the_terms( $product->get_ID(), 'pa_brand' );
echo '<pre>'; print_r( $brands ); echo '</pre>';
$cats = get_the_terms( $product->get_ID(), 'product_cat' );
echo '<pre>'; print_r( $cats ); echo '</pre>';
___________________________
RESULTS:
Array
(
[0] => WP_Term Object
(
[term_id] => 28
[name] => FKS
[slug] => fks
[term_group] => 0
[term_taxonomy_id] => 28
[taxonomy] => pa_model
[description] =>
[parent] => 0
[count] => 3
[filter] => raw
)
)
Array
(
[0] => WP_Term Object
(
[term_id] => 36
[name] => MAN
[slug] => man
[term_group] => 0
[term_taxonomy_id] => 36
[taxonomy] => pa_brand
[description] =>
[parent] => 0
[count] => 3
[filter] => raw
)
)
Array
(
[0] => WP_Term Object
(
[term_id] => 39
[name] => Truck
[slug] => truck
[term_group] => 0
[term_taxonomy_id] => 39
[taxonomy] => product_cat
[description] =>
[parent] => 0
[count] => 2
[filter] => raw
)
)
但如果页面不是单一产品,即http://example.com/?manage_product=206,我会同时关注pa_brand 和pa_model 的WP_Error。
$models = get_the_terms( $product_id, 'pa_model' );
echo '<pre>'; print_r( $models ); echo '</pre>';
$brands = get_the_terms( $product_id, 'pa_brand' );
echo '<pre>'; print_r( $brands ); echo '</pre>';
$cats = get_the_terms( $product_id, 'product_cat' );
echo '<pre>'; print_r( $cats ); echo '</pre>';
___________________________
RESULTS:
WP_Error Object
(
[errors] => Array
(
[invalid_taxonomy] => Array
(
[0] => Invalid taxonomy.
)
)
[error_data] => Array
(
)
[additional_data:protected] => Array
(
)
)
WP_Error Object
(
[errors] => Array
(
[invalid_taxonomy] => Array
(
[0] => Invalid taxonomy.
)
)
[error_data] => Array
(
)
[additional_data:protected] => Array
(
)
)
Array
(
[0] => WP_Term Object
(
[term_id] => 39
[name] => Truck
[slug] => truck
[term_group] => 0
[term_taxonomy_id] => 39
[taxonomy] => product_cat
[description] =>
[parent] => 0
[count] => 2
[filter] => raw
)
)
短代码
function frontend_add_product_form($product_id){
global $product_id;
$product = new WC_Product( $product_id );
$koostis = get_the_terms( $product_id, 'pa_brand', array( 'fields' => 'names' ) );
echo 'koostis<br><pre>'; print_r( $koostis ); echo '</pre>';
}
add_shortcode('frontend-add-product-form', 'frontend_add_product_form');
我已经被困了几个小时,无法弄清楚,我如何通过产品 ID 从自定义分类中获取相关术语?
【问题讨论】:
-
@Bhautik 他们没有任何工作.. 尝试了多个选项
-
你把这段代码放在哪里了?
-
@Bhautik 在用于产品表单的简码中。我已将此添加到我上面的帖子中
-
$koostis = get_the_terms( $product_id, 'pa_brand', array( 'fields' => 'names' ) );没有第三个参数。
标签: wordpress woocommerce taxonomy custom-taxonomy taxonomy-terms