我为具有此类结构的公司制作零售商列表。 您首先列出的是一个拥有该零售商城市的国家/地区。 这是通过我制作的自定义帖子类型连接的,简单地称为"零售商"然后我对这个自定义帖子类型进行了分类,该类型将国家/地区作为父项目,将城市作为子项目。
Country 1
-- City 1
--- Retailer 1
--- Retailer 2
-- City 2
--- Retailer 3
--- Retailer 4
Country 2
-- City 3
--- Retailer 5
-- City 4
---- Retailer 6
事情是我被困住了,并且不仅仅显示城市及其零售商,我希望能够包括这些国家,以便他们也能在循环中展示。如何添加代码以便我可以从分类中获取父项?
这是我的循环代码
$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>";
}
当前列表的图片:
分类结构的图像:
答案 0 :(得分:1)
试试这个
<?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
}
}
?>
有用的链接:https://codex.wordpress.org/Function_Reference/get_categories