【问题标题】:Wordpress: Display category and its childrenWordpress:显示类别及其子项
【发布时间】:2015-05-19 08:53:43
【问题描述】:

我正在为具有这种结构的公司制作零售商名单。 您首先列出一个拥有城市的国家/地区,该城市拥有零售商。 这是通过我创建的自定义帖子类型连接起来的,它简称为“零售商”,然后我对这种自定义帖子类型进行了分类,其中国家作为父项,城市作为子项。

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'] . "&nbsp;";
        echo "" . $store['adress'] . "&nbsp;";
        echo "" . $store['phone'] . "&nbsp;";
        echo "<a href='http://" . $store['website'] . "' target='_blank'>" . $store['website'] . "</a></p>";
    }

    echo "</div>";                
}

当前列表的图像:

分类结构图:

【问题讨论】:

    标签: php wordpress categories taxonomy children


    【解决方案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

    【讨论】:

    • 我试过这个 - 但它不能正常工作,它只显示 wordpress 附带的“未分类”类别的名称,我在这一行得到 php 错误,$child 是未定义的多变的。 "echo $child->name . "
      "; //获取子类别名称"
    • 对不起。在代码中的乳香。请再试一次。
    • 我认为这已经非常接近了——现在它向我展示了“未分类”的父类别和我分配给它的一个子类别。但我认为我需要将 id 添加到类别或我使用的分类中,以便获得正确的类别。
    • link 你传递参数作为你需要的结果。 code $args = array( 'type' => 'post', 'child_of' => 0, 'parent' => '', 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => 1, 'hierarchical' => 1, 'exclude' => '', 'include' => '', 'number' => '', 'taxonomy' => 'category', 'pad_counts' => 假);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-13
    • 2012-06-25
    • 1970-01-01
    相关资源
    最近更新 更多