How to fetch parent categories of a custom post
Here is the solution to fetch only the parent categories of a custom post type. As we know that sometimes it is required to register new post types with some custom taxonomy as well to fulfill our requirements. So, in a case we have a post type and we are using custom taxonomy for that post type. We have categories listing in parent and child manner.
In some situations we need to fetch the parent categories only then use the below mentioned code at your desired location :
<?php $parents_cat_list = get_terms('events', array( 'parent' => 0 ) ); // event is taxonomy here foreach( $parents_cat_list as $parent_cats ): // getting parent category name and its link echo '<a href="' . get_term_link( $parent_cats ) . '">' . $parent_cats->name . '</a>: '; endforeach; ?>
Where “events” is the custom taxonomy. In this way we will get only the parent categories of “events” taxonomy with link to their archive pages.