Get only the top level categories using get_categories() without foreach loop

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.

Read More

How can I modify the argument here?

Related posts

3 comments

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

  2. A native WordPress solution to return the CURRENT parent category.

    You do not need to use foreach outside the function…

    function primary_categories($arr_excluded_cats) {
    
    if($arr_excluded_cats == null) {
        $arr_excluded_cats = array();
    }
    
    $post_cats = get_the_category();
    
    $args = array(
      'orderby' => 'name',
      'order' => 'ASC',
      'parent' => 0
    );
    
        $primary_categories = get_categories($args);
    
        foreach ($primary_categories as $primary_category) {
    
            foreach ($post_cats as $post_cat) {
                if(($primary_category->slug == $post_cat->slug) && (!in_array($primary_category->slug, $arr_excluded_cats))) {
                    return $primary_category->slug;
                }
            }
        }
    }
    
    //if you have more than two parent categories associated with the post, you can delete the ones you don't want here
    $dont_return_these = array(
            'receitas','enciclopedico'
        );
    
    //use the function like this:
    echo primary_categories($dont_return_these);
    

    Comments:

    • if you only have one parent category by post, pass null instead of array
    • if you want another output instead of slug change this to return $primary_category-> slug;

Comments are closed.