Search code examples
phpwordpresscategorieswp-list-categories

Display top level wordpress categories


I have the following bit of code in my Wordpress template and I'd like to change it so it only displays the top-level categories, rather than all categories:

<?php
/**
 * Generate list of EDD categories to browse
 */

if ( $categories ) { ?>

    <div class="search-cats">
        <div class="search-cat-text">
            <?php _e( 'Or browse by category: ', 'checkout' ); ?>
        </div>
        <nav>
        <?php
            /**
             * Generate list of EDD category links
             */
            foreach ( $categories as $category ) {
                $link = get_term_link( $category, 'download_category');


                echo '<a href="' . esc_url( $link ) . '" rel="tag">' . $category->name . '</a>';
            }
        ?>
        </nav>
    </div>
<?php } ?>

Can someone help me?


Solution

  • Point is on parent => 0

    <?php
        /**
         * Generate list of EDD categories to browse
         */
        $args = array(
          'orderby' => 'name',
          'taxonomy' => 'download_category',
          'hide_empty' => 0,
          'parent' => 0
        );      
        $categories = get_categories($args);