代码之家  ›  专栏  ›  技术社区  ›  Kenshinh

将Woomerce产品标签slug作为数组获取

  •  1
  • Kenshinh  · 技术社区  · 8 年前

    如何将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);
    
        ?>
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   LoicTheAztec    8 年前

    你可以用 wp_get_post_terms() 相反,wordpress函数通过这种方式在一行代码的数组中获取术语slug

    $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>'; 
    
    推荐文章