[英]Wordpress: Display category and its 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
事情是我被困住了,不能只展示城市及其零售商,我希望能夠包括這些國家,以便它們也能在循環中展示。 如何添加代碼,以便可以從分類法中刪除父項?
這是我的循環代碼
$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>";
}
當前列表的圖像:
分類結構的圖像:
嘗試這個
<?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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.