I am new in wordpress, I am using wordpress Version 4.5, I like to display posts by subcategories, anyone please help how can I do this.
This is what I want
PARENT CATEGORY NAME
SUB CATEGORY 1
Post 1
Post 2
SUB CATEGORY 2
Post 3
Post 4
SUB CATEGORY 3
Post 5
Post 6
…
Thanks in advance
If I am not wrong you need this, you need double loop to fetch the posts under subcategories
this is how you get current page category
and then do this by using above catID
If you want to show all the categories that have subcategories with their respective subcategories that have posts with their respective posts, you can easily do that with the following function, as long as you know the slug of the taxonomy and the post type.
For example if you have a custom post type called ‘books’ and a custom taxonomy to classify your books called ‘topic’,
then you would call this function using
That will display a list of all the topics with their respective subcategories followed by the books that belong to each subcategory.
Something like
Drama
drama subcategory 1
drama subcategory 2
Comedy
comedy subcategory 1
comedy subcategory 2
The following is the code for this:
Then if you ONLY want to get the subcategories related to comedy, and you want to provide the ‘comedy’ slug as a filter to get something like:
comedy subcategory 1
comedy subcategory 2
Then you would call the following function like this:
And the function to do that will be: