Wordpress - Get only the top level categories using get_categories() without foreach loop
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.
set parent
to 0
$args = array(
'parent' => 0,
'hide_empty' => 0
);