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?

3 s
3

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.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *