Teege
Teege

Reputation: 101

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?

Upvotes: 1

Views: 792

Answers (2)

Marek Zavoronok
Marek Zavoronok

Reputation: 140

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);

Upvotes: 2

Chandra Kumar
Chandra Kumar

Reputation: 4205

You use your code like this by using get_categories(), you should try :

$args = array(
  'orderby' => 'name',
  'parent' => 0
);

parent (integer) Get direct children of this term (only terms whose explicit parent is this value). If 0 is passed, only top-level terms are returned. Default is an empty string.

Read more : http://codex.wordpress.org/Function_Reference/get_categories#Get_only_top_level_categories

Upvotes: 0

Related Questions