【问题标题】:Is there a way to get all the category slug of a woocommerce product有没有办法获得 woocommerce 产品的所有类别信息
【发布时间】:2019-12-13 22:16:28
【问题描述】:

我正在 wordpress 上使用 Isotope 和 Woocommerce 制作具有类别的产品动态图库,以对我使用其类别 slug 的产品进行排序,但是当产品具有多个类别时,我无法找到获取其他类别的方法,并且在这一点上,我什至不确定这是否可能。

我尝试使用 $product->get_categories(); 获取类别而不是 slug,但它返回一个包含类别的 <a href>

function gallery(){

    Print "<div class='button-group filter-button-group'>
            <button data-filter='*' >show all</button>
            <button data-filter='.music-album'>Music</button>
            <button data-filter='.movie-soundtrack'>Movies</button>
            <button data-filter='.game-soundtrack'>Games</button>
            </div>";
    Print"<div class='row'>";

    $products= new WP_Query(['post_type'=>'product']);

    if($products->have_posts()) : while ($products->have_posts()) : $products->the_post();

        $product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );
        $single_cat = array_shift( $product_cats );
        $category = $single_cat->slug.' '; // IF POSSIBLE : I want here to get all the slug of the actual product on the loop

        Print"<a class='grid-item ".$category."' href='".get_the_permalink()."'>";
        Print"<div class='card grid-item".$category."'>";
            Print"<img class='card-img-top'src='".get_field('image')."'alt='' width='200' height='200'>";
            Print"<h1 class='card-body'>"; the_title(); Print"</h1>";
        Print"</div>";
        Print"</a>";


    endwhile;endif; 

    Print"</div>";

}

所以理想情况下,我希望 $category 变量等于 category_slug1 category_slug2 category_slug3 .... 之类的值,如果产品有 10 个类别,我希望它能够找到所有类别。

【问题讨论】:

    标签: php woocommerce word slug jquery-isotope


    【解决方案1】:
    $slugs = array();
    $product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );
    foreach ($product_cats as $product_cat){
        $slugs[] = $product_cat->slug;
    }
    

    这会将所有 slugs 添加到 slugs 数组中。然后你可以使用 implode() 将它粘到一个字符串上。

    【讨论】:

      猜你喜欢
      • 2018-09-02
      • 2020-11-04
      • 2019-09-03
      • 2017-04-01
      • 2012-01-07
      • 1970-01-01
      • 1970-01-01
      • 2022-01-04
      • 2018-01-23
      相关资源
      最近更新 更多