Wordpress 在数组中获取产品标签
我想在数组中获取woocommerce产品的产品标签,以便对其执行if/else逻辑(在_数组中),但我的代码不起作用:Wordpress 在数组中获取产品标签,wordpress,woocommerce,Wordpress,Woocommerce,我想在数组中获取woocommerce产品的产品标签,以便对其执行if/else逻辑(在_数组中),但我的代码不起作用: <?php $aromacheck = array() ; $aromacheck = get_terms( 'product_tag') ; // echo $aromacheck ?> 您需要在数组中循环并创建一个单独的数组来检查数组中的,因为get\u termsreturnobject $terms = get_terms( 'product_
<?php
$aromacheck = array() ;
$aromacheck = get_terms( 'product_tag') ;
// echo $aromacheck
?>
您需要在数组中循环并创建一个单独的数组来检查数组中的
,因为get\u terms
returnobject
$terms = get_terms( 'product_tag' );
$term_array = array();
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
$term_array[] = $term->name;
}
}
所以,在循环通过数组之后
您可以在数组()中使用假设
$term\u数组
包含标签黑色
if(in_array('black',$term_array)) {
echo 'black exists';
} else {
echo 'not exists';
}
我必须将args数组解析为get_terms函数。也许这对其他人也有帮助
$args = array(
'number' => $number,
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
'include' => $ids
);
$product_tags = get_terms( 'product_tag', $args );
$args=array(
“number”=>$number,
'orderby'=>$orderby,
“订单”=>$order,
'hide_empty'=>$hide_empty,
'包括'=>$id
);
$product\U tags=获取条款('product\U tag',$args);
//只有当我们有一些标签时才开始
如果($product_tags&&!is_wp_error($product_tags)){
//创建一个列表来保存我们的标签
回声“”;
//我们为每个标记创建一个列表项
foreach($product_标记为$tag){
$tag\u title=$tag->name;//标记名
$tag\u link=get\u term\u link($tag);//标记存档链接
echo';
}
回声“
”;
}
它确实返回一个对象数组。你不能echo
一个对象数组…这真的很有用谢谢,它可以检查数组,并获取值(愚蠢的我使用echo…),但不幸的是它列出了所有标签,而不仅仅是产品本身使用的标签。它可以被重新编码吗?只列出特定的产品标签?你从来没有要求过这个…如果你需要为特定的产品这样做,你可以使用。谢谢,就是这样,我把$terms=get_the_terms($post->ID,'product_tag')代码>而且效果很好!我编辑了我的帖子以包含完整的解决方案。好的,对不起,我忘了。谢谢你的帮助!请在您的答案中添加描述!请添加一些描述以解释您的代码。
$args = array(
'number' => $number,
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
'include' => $ids
);
$product_tags = get_terms( 'product_tag', $args );
$args = array(
'number' => $number,
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
'include' => $ids
);
$product_tags = get_terms( 'product_tag', $args );
//only start if we have some tags
if ( $product_tags && ! is_wp_error( $product_tags ) ) {
//create a list to hold our tags
echo '<ul class="sb-tag">';
//for each tag we create a list item
foreach ($product_tags as $tag) {
$tag_title = $tag->name; // tag name
$tag_link = get_term_link( $tag );// tag archive link
echo '<li class="sidebar-list-tag" ><a href="'.$tag_link.'"> <span>'.$tag_title.' </span></a></li>';
}
echo '</ul>';
}
global $product;
$tags = $product->tag_ids;
foreach($tags as $tag) {
echo get_term($tag)->name;
}