Skip to content
Advertisement

WP Get IDs of parent categories only

The snippet below works fine but I’ve hard coded the parent categories IDs, Now I’m looking for a way to get rid of hard coded IDs.

      <div class="row">
                    <?php
                     $catsArray = array(176, 175); // This line need to be dynamic and the IDs are parent categories.
                      $categories = get_terms(
                              array(
                          'hide_empty' => false,
                          'include' => $catsArray, 'orderby'  => 'include'

                      ) );

                      foreach ($categories as $key => $cat) {
        
                      $cat_thumb_id = get_term_meta( $cat->term_id, 'thumbnail_id', true );



                      $term_link = get_category_link( $cat->term_id );
                     ?>
                    <div class="col-md-6">
                      <div class="sellers-wrap is-revealing">
                        <figure>
                          <img src="<?php echo $cat_img; ?>" alt="" class="img-fluid">
                        </figure>
                        <div class="sellers-text">
                            <p><strong><?php echo $cat->name; ?></strong></p>
                          
                        </div>
                      </div>
                    </div>
                 <?php } ?>
                  </div>

Advertisement

Answer

If 0 is passed, only top-level terms are returned.

$categories = get_terms( 
 'category', 
 array('parent' => 0)
);
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement