Ender91
Ender91

Reputation: 71

How to grab Category + children in my ForEach Loop

Modal Bootstrap filter box with categories + children

Hello all, I'm trying to create a Foreach in wordpress for the bootstrap modal to grab the category + children. I'm working with the Search and Filter plugin Pro so I need the url to echo out the category name but not its url at the end of the url which i'm struggling to figure out how to add it at the end, example below. Any help would be greatly appreciated Thank you all:

 <?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>';
            }
            ?> 

Upvotes: 0

Views: 537

Answers (1)

DubVader
DubVader

Reputation: 1072

You have a few issues with your query and loop. Try this:

<?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 } 

Upvotes: 1

Related Questions