显示顶级 wordpress 类别

Display top level wordpress categories

我的 Wordpress 模板中有以下代码,我想更改它以便它只显示顶级类别,而不是所有类别:

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

有人可以帮助我吗?

点在父对象上 => 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);

您通过使用 get_categories() 来使用您的代码,您应该尝试:

$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.

阅读更多:http://codex.wordpress.org/Function_Reference/get_categories#Get_only_top_level_categories