简体   繁体   中英

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'] . "&nbsp;";
        echo "" . $store['adress'] . "&nbsp;";
        echo "" . $store['phone'] . "&nbsp;";
        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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM