Ciprian
Ciprian

Reputation: 3226

Exclude category children get_categories

I'm not sure why this isn't working. I want to exclude categories 406, 982, and 1319 and their children from the list. I don't see any other arguments on wp codex. Does 'exclude' automatically exclude children? Is there another way to do this?

**Edit: I can't even limit the results to 10.

<?php
$args = array(
    'exclude' => '406,982,1319',
    '#'       => '10'
);
$sidebar_artists = get_categories($args);

echo "<ul>";
foreach ($sidebar_artists as $sidebar_artist) {

    echo '<li class="cat-item">' . $sidebar_artist->category_nicename . '</li>';
}
echo "</ul>";
?>

Upvotes: 2

Views: 3374

Answers (2)

timhc22
timhc22

Reputation: 7451

Set parent to 0:

<?php
$args = array(
  'orderby' => 'name',
  'parent' => 0
  );
$categories = get_categories( $args );
foreach ( $categories as $category ) {
    echo '<a href="' . get_category_link( $category->term_id ) . '">' . $category->name . '</a><br/>';
}
?>

https://codex.wordpress.org/Function_Reference/get_categories

Upvotes: 3

Shumail
Shumail

Reputation: 3143

it looks like it should be working but did you try any alternative ? like is_category in if/else? or try exclusion by category sluginstead of IDs for sake of debugging....

WP Codex Reference for is_category

Upvotes: 0

Related Questions