How to Display Only WordPress Parent Categories

For the beginners in WordPress, i would say that WordPress by default comes with the categories functions. It’s up to the owner of WordPress powered site whether he uses this function or not. For blogging it is somewhat a necessity to categorize your posts in categories (if you do post articles etc about different topics).
Categories let us organize different material separately and help the site visitors in finding the posts they are looking for easily.
Now to the main sole of this post, there are times when we want to manipulate wordpress categories according to our way/thinking other than their default functionality.
If you just want to display categories list OR drop-down list of categories, you are welcome to use  wp_list_categories($args) and wp_dropdown_categories( $args ).

We want a function that does not format the result (categories), which let us style and format the categories as we like. Hey one moment WordPress have provided us such a function  get_categories().

get_categories function accept different arguments, if you want to know which arguments can we pass to this function visit WordPress function reference page  for get_categories() .
According to WordPress codex to display parent categories pass the argument parent=>0 to get_categories function, but this does displayed all the categories with me.

This code section is taken from WordPress function reference page:
< CODE  1 >
$categories = get_categories( array(
    'orderby' => 'name',
    'parent'  => 0
) );
 
foreach ( $categories as $category ) {
    printf( '<a href="%1$s">%2$s</a><br />',
        esc_url( get_category_link( $category->term_id ) ),
        esc_html( $category->name )
    );
}
 For example i have these categories in the below image with three child categories namely Cats, anim and Sparrow we will run both the codes for displaying our desired result:
categories1
By passing the argument ‘parent’=>0 as in the code above i got this result, which also contains child categories .
< CODE 1 > result after execution
categories2
The solution to the above problem is if we get all the categories by using get_categories() function than we can also check each category if it is a parent or not.  In our case we will check $single_cat->parent,  if this equal to 0 than it is parent otherwise not.

simply  place this condition ( if($single_cat->parent < 1) ) in the start of foreach loop and you will get only parent categories.
Using get_categories() function to display only parent categories.

< CODE 2 >

// get_categories() function will return all the categories
$upaae_categories = get_categories( array(
 'orderby' => 'name',
 'order' => 'ASC'
) );

foreach( $upaae_categories as $single_cat ) {
 if($single_cat->parent < 1) // Display if parent category and exclude child categories
 {
 $category_link = sprintf( '<a href="%1$s" alt="%2$s">%3$s</a>',
 esc_url( get_category_link( $single_cat->term_id ) ),
 esc_attr( sprintf( 'View all posts in %s', 'textdomain' ), $single_cat->name ),
 esc_html( $single_cat->name ));

 echo sprintf( $category_link ).'</br>';

 }
} 

By using the above code only parent categories will be displayed like the result in this image:

< CODE 2 result after execution >

categories3

This code (< CODE 2 >)works in any default template files OR custom template files and in plugins, if you are facing any problem in executing this code feel free to comment and i will try my best to help.




Leave a Reply

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


1822 Views