admin管理员组文章数量:1330657
As the title suggests, I am trying to get the sub-category of the Genre category.
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
I know what my product/song ID is. I know what the Genre category ID is.
I struggle with the following:
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);
}
}
When running print_r($assoc_categories);
of the above, it simply returns an array with all the sub-categories of parent "Genre". This is however not right as I need to get the name of only the relevant sub-category associated with the specific product/song.
Say for example, the genre of the song is "Rock". Rock is a sub-category of the "Genre" category. The sub-category Rock is associated with the product/song name.
How can I access only the relevant sub-category of Genre in this instance?
I am struggling a bit to find answers online, unless I keep looking for the wrong solution, by searching the wrong terms...
Any help would be greatly appreciated!
Please do keep in mind that I am still learning about coding with WordPress
As the title suggests, I am trying to get the sub-category of the Genre category.
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
I know what my product/song ID is. I know what the Genre category ID is.
I struggle with the following:
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);
}
}
When running print_r($assoc_categories);
of the above, it simply returns an array with all the sub-categories of parent "Genre". This is however not right as I need to get the name of only the relevant sub-category associated with the specific product/song.
Say for example, the genre of the song is "Rock". Rock is a sub-category of the "Genre" category. The sub-category Rock is associated with the product/song name.
How can I access only the relevant sub-category of Genre in this instance?
I am struggling a bit to find answers online, unless I keep looking for the wrong solution, by searching the wrong terms...
Any help would be greatly appreciated!
Please do keep in mind that I am still learning about coding with WordPress
Share Improve this question asked Jul 13, 2020 at 16:39 rdkrdk 11 Answer
Reset to default 0For more info, check this thread over on Stack Overflow.
To answer my own question, I have created a more comprehensive bit of code that builds an array containing the following:
- Artist Name, ID, Slug >
- Each Albums Name, ID, Slug, Year, Genre >
- Each Songs Name, ID, Slug, SKU, Link to a preview of the Track
- Each Albums Name, ID, Slug, Year, Genre >
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);
Hopefully this may help another Noob that comes across the same challenge somewhere down the line.
本文标签: phpGet only relevant subcategory of category quotGenrequot that applies to current product only
版权声明:本文标题:php - Get only relevant sub-category of category "Genre" that applies to current product only 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742262624a2442798.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论