php - 获取 woocommerce 产品标签 slugs 作为数组

标签 php wordpress woocommerce product custom-taxonomy

如何正确地将 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/

相关文章:

php - JSON/数组到 MySQL

php - WordPress - 导出特色图像 mysql

php - Woocommerce 中特定产品的购物车批量折扣

php - 将格式化的地址状态代码更改为 Woocommerce 订单中的状态名称

php - WooCommerce - 在 pdf 发票的订单循环中转置购物车循环

php - jQuery 从选定的复选框中获取值

php - 将 Javascript 函数传递给 PHP 文件

css - 导航列表未正确居中

wordpress - 删除 WordPress 帖子中的 <p> 和 <br/> 标签

php - www 不重定向到非 www .htaccess Wordpress