How to display only parent category in your WordPress post loop?

How to display only parent category in your WordPress post loop?

In this article, we will show you how to display only parent category in your WordPress post loop. Simply open your single.php file or anywhere else that you want to utilize this code, and then replace the following code or something similar to it: Now this will let you display only the parent category in your WordPress post loop.

Where can I find the parent category ID?

The parent category ID can easily be identified by navigating to WooCommerce > Categoriess, selecting the parent category concerned, and clicking to edit the category. The id would be revealed in the url. Once you have updated the parent category ID, you can refresh the page and the sub-categories would be displayed.

Why are there parent and child categories in WordPress?

Built upon WordPress’ parent category and child category for posts that goes way back, WooCommerce has allowed its product categories to also have such kind of hierarchy. This feature provides an efficient means of organization especially for larger stores which sell a variety of products across different product types.

How to display the subcategories from a specific category in PHP?

However, as the number of categories and subcategories increases, it is more attractive to get subcategories of a certain category programmatically or through PHP code. In this way, you would be able to show a list of subcategories on the category pages themselves.

Which is the shortcode for display of posts?

The [su_posts] shortcode is intended for display of posts, pages, and various post types. You can display posts from a specific category or by a specific tag. You can also choose multiple taxonomies and select the number of displayed posts. This shortcode uses WP_Query class.

How to display posts from a particular category in WordPress?

To display posts from a particular category follow the steps: Copy the below code snippet and add it in functions.php file located at wp-content/themes/your-theme/ . Then add this shortcode in page, post, or widgets by passing category id and other parameters in it. cat = Category id for which you want to display posts.

Why do I have child categories in WordPress?

If you are creating a site that uses a lot of child categories to categorize the content, then this can become a problem because the_category code will create a lot of clutter. For example, it will show as “Filed in Blogs, Genesis Framework, 2 Column Theme etc”.