wordpress Woocommerce 获取数组中的产品标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31904758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:08:07  来源:igfitidea点击:

Woocommerce Get product tags in array

wordpresswoocommerce

提问by Gas

I want to get the product tags of the woocommerce products in an array, for doing if/else logic with it (in_array), but my code doesn't work:

我想在一个数组中获取 woocommerce 产品的产品标签,以便对其进行 if/else 逻辑(in_array),但我的代码不起作用:

<?php 

$aromacheck = array() ; 
$aromacheck = get_terms( 'product_tag') ; 
// echo $aromacheck

?>

When echoing $aromacheck, I only get empty Array, though the product tags are existing - visible in the post class.

当回显 $aromacheck 时,我只得到空数组,尽管产品标签存在 - 在 post 类中可见。

How can I get the product tags in an array correctly?

如何正确获取数组中的产品标签?

Solution (thanks Noman and nevius):

解决方案(感谢 Noman 和 nevius):

/* Get the product tag */
$terms = get_the_terms( $post->ID, 'product_tag' );

$aromacheck = array();
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
    foreach ( $terms as $term ) {
        $aromacheck[] = $term->slug;
    }
}

/* Check if it is existing in the array to output some value */

if (in_array ( "value", $aromacheck ) ) { 
   echo "I have the value";
} 

回答by Noman

You need to loop through the array and create a separate array to check in_arraybecause get_termsreturn objectwith in array.

您需要遍历数组并创建一个单独的数组进行检查,in_array因为get_terms返回object数组中。

$terms = get_terms( 'product_tag' );
$term_array = array();
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
    foreach ( $terms as $term ) {
        $term_array[] = $term->name;
    }
}

So, After loop through the array.

所以, After 循环遍历数组。

You can use in_array().
Suppose $term_arraycontains tag black

您可以使用in_array()
假设$term_array包含标签黑色

if(in_array('black',$term_array)) {
 echo 'black exists';
} else { 
echo 'not exists';
}

回答by Unicco

I had to parse an args-array to the get_terms function. Maybe this help other aswell.

我必须将 args 数组解析为 get_terms 函数。也许这也有助于其他人。

$args = array(
    'number'     => $number,
    'orderby'    => $orderby,
    'order'      => $order,
    'hide_empty' => $hide_empty,
    'include'    => $ids
);

$product_tags = get_terms( 'product_tag', $args );

回答by Abdel Rahman Kamhawy

$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>';
}