[英]Wordpress: Display category and its children
I'm making a retailer list for a company with a structure like this. 我正在为具有这样结构的公司制作零售商清单。 That you first list a country which has cities which has it's retailers. 您首先列出了一个拥有其零售商的城市的国家。 This is connected by a custom post type that I made which called simply "Retailers" And then I made a taxonomy to this custom post type which has the Countries as parent items and the cities as children. 这是由我创建的自定义帖子类型(简称为“零售商”)关联的,然后我对该类自定义帖子进行了分类,该类以国家为父项,城市为子项。
Country 1
-- City 1
--- Retailer 1
--- Retailer 2
-- City 2
--- Retailer 3
--- Retailer 4
Country 2
-- City 3
--- Retailer 5
-- City 4
---- Retailer 6
The thing is I'm stuck and can't display more than just the cities and their retailers, I want to be able to include the countries so that they show in the loop as well. 事情是我被困住了,不能只展示城市及其零售商,我希望能够包括这些国家,以便它们也能在循环中展示。 How can I add in the code so that I can get out the parent item from the taxonomy? 如何添加代码,以便可以从分类法中删除父项?
This is my code for the loop 这是我的循环代码
$args = array( 'post_type' => 'drsj_retailer', 'posts_per_page' => -1);
$loop = new WP_Query( $args );
$allCities = array();
while ( $loop->have_posts() ) : $loop->the_post();
$post_id = get_the_ID();
$args = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all');
$terms = wp_get_post_terms( $post_id, 'retailer_city', $args );
$store = array();
$store['title'] = get_the_title();
$store['adress'] = get_field('reseller_adress');
$store['phone'] = get_field('reseller_phone');
$store['website'] = get_field('reseller_website');
$allCities[$terms[0]->name][] = $store;
endwhile;
foreach($allCities as $cityName => $stores) {
echo "<div class='resellerEntry'>";
echo "<h3 class='retailerCityTitle'>" . $cityName . "</h3>";
foreach($stores as $store) {
echo "<p>" . $store['title'] . " ";
echo "" . $store['adress'] . " ";
echo "" . $store['phone'] . " ";
echo "<a href='http://" . $store['website'] . "' target='_blank'>" . $store['website'] . "</a></p>";
}
echo "</div>";
}
Image of the current list: 当前列表的图像:
Image of the taxonomy structure: 分类结构的图像:
Try This 尝试这个
<?php
//***----------------Parent cat args---------------------***/
$Parentcatargs = array(
'orderby' => 'name',
'order' => 'ASC',
'use_desc_for_title' => 1,
'hide_empty' => 0,
'parent' => 0
);
$category = get_categories($Parentcatargs);
//print_r($category); //Return Array
foreach ($category as $Parentcat) {
echo $Parentcat->name . "<br>"; //Get Parent Category Name
//***----------------child cat args---------------------***/
$childargs = array(
'child_of' => $Parentcat->cat_ID,
'hide_empty' => 0,
'parent' => $Parentcat->cat_ID
);
$childcategories = get_categories($childargs);
//print_r($childcategories); //Return Array
foreach ($childcategories as $childcat) {
echo $childcat->name . "<br>"; //Get child Category Name
}
}
?>
Useful Link: https://codex.wordpress.org/Function_Reference/get_categories 有用的链接: https : //codex.wordpress.org/Function_Reference/get_categories
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.