WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Very simple Question: Here is my get_categories() code:

<?php $args = array(
                        'show_option_all'   => '',
                        'show_option_none'  => '',
                        'orderby'           => 'ID',
                        'order'             => 'ASC',
                        'show_count'        => 0,
                        'hide_empty'        => 0,
                        'exclude'           => '1',
                        'hierarchical'      => 0,
                        'depth'             => 1,
                        'number'            => 12

<?php $categories = get_categories( $args ); ?>
                    <div class="middle-left">
                            <?php for( $i=0; $i<4; $i++ ) {
                                echo "<li>" . $categories[$i]->{'name'} . "</li>";
                            } ?>
                    <div class="middle-middle">
                            <?php for( $i=4; $i<8; $i++ ) {
                                echo "<li>" . $categories[$i]->{'name'} . "</li>";
                            } ?>
                    <div class="middle-right">
                            <?php for( $i=8; $i<12; $i++ ) {
                                echo "<li>" . $categories[$i]->{'name'} . "</li>";
                            } ?>

After a long search I got the way how to get into the stdClass object without foreach loop and I want to proceed with this way. But I want only the top level categories, no subcats.

How can I modify the argument here?

share|improve this question
downvoted because the Codex codex.wordpress.org/Function_Reference/get_categories should have given you the answer – Michael Dec 29 '13 at 0:01
@Michael Can't downvote your comment. :( If so, you failed to point that out, I'm afraid. :( – Mayeenul Islam Dec 29 '13 at 6:32
up vote 5 down vote accepted

Use get_terms with a parent argument. From the Codex page for that function, emphasis mine:

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.

Untested, but this should do it.

$categories = get_terms( 
   array('parent' => 0)

Of course, you need to add any other arguments you require.

share|improve this answer
Tested now. Nice solution. Thanks. :) – Mayeenul Islam Dec 29 '13 at 7:02

set parent to 0

$args = array(
  'parent' => 0,
  'hide_empty' => 0
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.