仅获取仅适用于当前产品的类别“流派”的相关子类别

时间:2020-07-13 作者:rdk

正如标题所示,我正在尝试获得流派类别的子类别。

My WooCommerce category structure looks like this for example:

Genre, Genre > Rock, Artist, Artist > Abigail Jay > Hulking Depend Weigh, Album > Hulking Depend Weigh, Year, Year > 2014

我知道我的产品/歌曲ID是什么。我知道类型分类ID是什么。

我在努力解决以下问题:

global $product;
$terms = get_the_terms($product->get_id(), \'product_cat\');
foreach($terms as $term)
{
    if($term->slug == "genre")
    {
        $_id = $term->term_id;
        $_s = $term->slug;
        $_ar = array(\'parent\' => $_id);
        $assoc_categories = get_terms(\'product_cat\', $_ar);
    }
}
运行时print_r($assoc_categories); 其中,它只返回一个数组,其中包含父类的所有子类别;流派;。然而,这是不对的,因为我只需要获得与特定产品/歌曲相关的相关子类别的名称。

例如,这首歌的体裁是;摇滚乐;。岩石是;流派“;类别子类别Rock与产品/歌曲名称相关联。

在这种情况下,我如何只能访问相关的流派子类别?

我正努力在网上找到答案,除非我一直通过搜索错误的术语来寻找错误的解决方案。。。

任何帮助都将不胜感激!

Please do keep in mind that I am still learning about coding with WordPress

1 个回复
SO网友:rdk

有关详细信息,请查看上的此线程Stack Overflow.

为了回答我自己的问题,我创建了一段更全面的代码,用于构建包含以下内容的数组:

艺术家姓名、ID、Slug><每个专辑的名称、ID、Slug、年份、流派><每个歌曲的名称、ID、Slug、SKU、链接到曲目预览

if((!empty($_SERVER[\'HTTPS\']) && $_SERVER[\'HTTPS\'] !== \'off\') || $_SERVER[\'SERVER_PORT\'] == 443)
{
    $GLOBALS[\'base_href\'] = \'https://\'.$_SERVER[\'SERVER_NAME\'].\'/\';
}
else
{
    $GLOBALS[\'base_href\'] = \'http://\'.$_SERVER[\'SERVER_NAME\'].\'/\';
} // END OF: if((!empty($_SERVER[\'HTTPS\'...
$main_category = get_queried_object();
$main_category_id = $main_category->term_id;
$main_category_name = $main_category->name;
$main_category_slug = $main_category->slug;
foreach(get_terms(\'product_cat\',  $main_category_id) as $term)
{
    if($term->slug == "genre")
    {
        foreach(get_terms(\'product_cat\', [\'parent\' => $term->term_id]) as $_gt)
        {
            $genre_dict[] = $_gt->name;
        }
    } // END OF: if($term->slug == "genre"...
    if($term->slug == "year")
    {
        foreach(get_terms(\'product_cat\', [\'parent\' => $term->term_id]) as $_gt)
        {
            $year_dict[] = $_gt->name;
        }
    } // END OF: if($term->slug == "year"...
} // END OF: foreach(get_terms(\'product_cat\',  $sub_category_id...

$_ARTIST_OBJ = 
[
    \'artist_id\' => $main_category->term_id,
    \'artist_name\' => $main_category->name,
    \'artist_slug\' => $main_category->slug
];
foreach(get_terms(\'product_cat\', [\'parent\' => $main_category_id]) as $sub_category)
{
    
    $sub_category_id = $sub_category->term_id;
    $sub_category_name = $sub_category->name;
    $sub_category_slug = $sub_category->slug;

    $_ARTIST_OBJ[\'artist_albums\'][$sub_category->slug] = 
    [
        \'album_id\' => $sub_category->term_id,
        \'album_name\' => $sub_category->name,
        \'album_slug\' => $sub_category->slug,
        \'album_year\' => "",
        \'album_genre\' => "",
        \'album_artwork\' => $GLOBALS[\'base_href\'].\'wp-content/uploads/album_images/\'.$sub_category->slug.\'.jpg\',
        \'album_tracks\' => []
    ];

    $product_loop = new WP_Query
    (
        [
            \'post_type\' => \'product\',
            \'product_cat\' => $sub_category_name,
            \'field\' => $sub_category_id
        ]
    );
    if($product_loop->have_posts())
    {
        while($product_loop->have_posts())
        {
            $product_loop->the_post();
            global $product;
            $_ARTIST_OBJ[\'artist_albums\'][$sub_category->slug][\'album_tracks\'][$product->get_slug()] = 
            [
                \'track_id\' => $product->get_id(),
                \'track_name\' => $product->get_name(),
                \'track_slug\' => $product->get_slug(),
                \'track_sku\' => $product->get_sku(),
                \'track_preview\' => $GLOBALS[\'base_href\'].\'wp-content/uploads/audio/samples/\'.strtolower(str_replace(array(" ", "-"), "", $product->get_name().$product->get_sku())).\'-sample.mp3\'
            ];
            foreach(get_the_terms($product->get_id(), \'product_cat\') as $term)
            {
                if(in_array($term->name, $genre_dict))
                {
                    $_ARTIST_OBJ[\'artist_albums\'][$sub_category->slug][\'album_genre\'] = $term->name;
                } // END OF: if(in_array($term->name, $genre_dict...
                if(in_array($term->name, $year_dict))
                {
                    $_ARTIST_OBJ[\'artist_albums\'][$sub_category->slug][\'album_year\'] = $term->name;
                } // END OF: if(in_array($term->name, $year_dict...
                
            }

        } // END OF: while($product_loop->have_posts(...
    }
    else
    {
        echo("<p>There are not products...</p>");
    } // END OF: if($product_loop->have_posts(...
} // END OF: foreach(get_terms(\'product_cat\', [\'parent\' => $main_category_id...

print_r($_ARTIST_OBJ);
希望这能帮助另一个遇到同样挑战的Noob。