Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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">
                        <ul>
                            <?php for( $i=0; $i<4; $i++ ) {
                                echo "<li>" . $categories[$i]->{'name'} . "</li>";
                            } ?>
                        </ul>
                    </div>
                    <div class="middle-middle">
                        <ul>
                            <?php for( $i=4; $i<8; $i++ ) {
                                echo "<li>" . $categories[$i]->{'name'} . "</li>";
                            } ?>
                        </ul>
                    </div>
                    <div class="middle-right">
                        <ul>
                            <?php for( $i=8; $i<12; $i++ ) {
                                echo "<li>" . $categories[$i]->{'name'} . "</li>";
                            } ?>
                        </ul>
                    </div>

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

1 Answer 1

up vote 2 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( 
   'category', 
   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

Your Answer

 
discard

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.