如何正确地将 woocommerce 标签 slug 名称获取到数组?我正在使用以下代码,但它没有输出任何内容。
<?php
$terms = get_the_terms( $post->ID, 'product_tag' );
$sluglist = array();
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
$sluglist[] = $term->slug;
}
}
echo count($sluglist);
?>
最佳答案
您可以使用wp_get_post_terms()
WordPress 函数以这种方式通过一行代码获取数组中的术语 slugs
$term_slugs = wp_get_post_terms( get_the_id(), 'product_tag', array( 'fields' => 'slugs' ) );
// The term slugs count
echo count($term_slugs);
// Testing: The raw output (preformatted)
echo '<pre>'; print_r($term_slugs); echo '</pre>';
关于php - 获取 woocommerce 产品标签 slugs 作为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51395201/