如何将woomerce标签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函数通过这种方式在一行代码的数组中获取术语slug
wp_get_post_terms()
$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>';