4
votes

i'm trying to create a category list, but i only want to list the parent categories and not the child categories. How can i do this? so far i've created a list, which list all parent and child categories.

function categoryList() {


  $args = array(
  'orderby' => 'name',
  'order' => 'ASC'
  );
$categories = get_categories($args);

  $output .= '<ul class="category-list">';
  foreach($categories as $category) { 
          if ($category){
          $output .= '<li><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a>';
          }
  } 

  $output .= '</li>';
  $output .= '</ul>';

  return $output;

}
5

5 Answers

8
votes

By parent categories, I assume you mean top-level categories. This is actually documented on the Codex page for get_categories: You should call get_categories with parent => 0

$args = array(
  'orderby' => 'name',
  'order' => 'ASC',
  'parent' => 0
);
$categories = get_categories($args);
3
votes

List only top-level (parent) categorizes in wordpress. The option 'hide_empty' => 0 makes sure to list even empty top-level categories.

        $args = array(
                'orderby' => 'name',
                'order' => 'ASC',
                'parent'   => 0,
                'hide_empty' => 0,
                //'exclude'   => '7',
                // optional you can exclude parent categories from listing
         );

        $categories = get_categories( $args );
0
votes

A native Wordpress solution to return the CURRENT parent category, excluding unwanted categories:

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;
0
votes

use this:

$categories = get_categories( [ 'parent'=> id_parent ,'hide_empty' => 0,] );
0
votes

The code below will give us the parent cat name and the URL.

function ns_primary_cat() {
  $cat_now = get_the_category();
  $cat_now = $cat_now[0];
  if ( 0 == $cat_now->category_parent ) {
     $catname = '<span class="category"><a href="' . get_category_link( $cat_now->term_id ) . '">' . $cat_now->name . '</a></span>';
  } else {
    $parent_id = $cat_now->category_parent;
    $parent_cat = get_category( $parent_id );
    $catname = '<span class="category"><a href="' . get_category_link( $parent_id ) . '">' . $parent_cat->name . '</a></span>';
  }
  return $catname;
}