如何在我的 ForEach 循环中获取 Category + children

How to grab Category + children in my ForEach Loop

Modal Bootstrap filter box with categories + children

大家好,我正在尝试在 wordpress 中为 bootstrap 模态创建一个 Foreach 来抓取类别 + 子项。我正在使用 Search and Filter plugin Pro,所以我需要 url 来回显类别名称,而不是 url 末尾的 url 我正在努力弄清楚如何在最后添加它,示例如下。任何帮助将不胜感激谢谢大家:

 <?php
             $categories =   wp_list_categories( array(
             'orderby'            => 'id',
             'use_desc_for_title' => false,
             'child_of'           => 216
            ) );
            foreach($category as $categories) {
            echo '<div class="col-md-3"><a href="<?php echo get_site_url(); ?>/our-companies/?_sft_category=**category name!** "></a></div>';
            }
            ?> 

您的查询和循环存在一些问题。试试这个:

<?php

    $categories = get_terms( array(

      'taxonomy' => 'category', // set the taxonomy
      'orderby' => 'ID', // orderby cat ID
      'parent' => 216 // set parent ID to get child cats

       )
     );


    foreach($categories as $cat) {

    $cat_slug = $cat->slug; // get category slug
    $site_url = get_site_url(); // get site url
    $cat_url = $site_url . '/our-companies?_sft_category=' . $cat_slug; // put url together

    ?>

    <div class="col-md-3">

      <!-- echo your url in to your href -->

      <a href="<?php echo $cat_url; ?>">Link text</a>

    </div>    

    <?php }