Ok so i’ve got categories like setup like this.
Products
Category A
Sub Category 1
Sub Category 2
Category B
Category C
What i need is to have the Products category page showing the main Categories (A,B,C).
The main Category pages (A,B,C) showing their sub Categories (1,2).
Then the Sub Categories (1,2) showing my custom post type i.e the actual products.
I want to avoid using a category-slug.php template because when someone adds a new category i want it to show automatically on the correct page, based on what the parent is.
I thought about using a category.php template but i don’t know if this is possible from one template or what code i would need.
The first category pages (Products) will only display the main Categories (A,B,C). It won’t be showing any posts just a list of category links in the main section. So i can style them so they are like big buttons on the page. The same will be for Category A,B,C. Show the sub categories.
The last category page (sub category 1,2) will show my custom post type (the actual products) in some sort of list. This page won’t show any other categories just the Products in the main section.
Any help on how i can achieve this would be appreciated.
Thanks
UPDATE:
I’ve managed to get the category template to show the right categories. It seems to be working but i’ve not tested it properly yet.
The problem i now have is displaying my custom post types on the deepest level categories. So it will show things like the custom post type title, thumbnail, maybe an excerpt.
Here’s the code i’m using.
<?php // list child categories
$cat_id = get_query_var('cat');
$catlist = wp_list_categories('depth=1&hide_empty=0&echo=0&orderby=id&title_li=&child_of=' . $cat_id);
if ($catlist) {
echo $catlist;
} else {
echo "Please don't print No categories";
} ?>
Currently on the deepest level category page it just displays ‘No Categories’. I need to get that page showing my custom post types that belong to that category.
I managed to do something like you are describing, but for a custom taxonomy.. with a little snippet of extra code. No doubt there is a much better way to write the code, and on top of that there is most likely a much more efficient way to accomplish this.
But this worked, and is working on an active website. I will describe what I did and you can modify the code for your own needs:
My problem:
Like you, I wanted a universal page that would show whatever taxonomy term I sent to it, instead of having to create a taxonomy-slug.php file for every new term in my taxonomy.
To clarify: my Custom Post Type is Videos, and my custom taxonomy is Video Types
My solution
I managed to grab the taxonomy term from the listing of that taxonomy(grabs when someone clicks on that term) and then sends it to an archive page for that particular custom post type. It reads that term and then just shows the post type using that taxo0nmy term
My Code
Found this on the web somewhere. It gets pasted into the functions file:
On the home-page, I list all of the different “Video Types” as clickable links using a wp-query to show the latest post for each of the different terms in the taxonomy.
The Query
The Links
I then make each term in the loop a link like so:
which then sends that term name along with the URL to the archives-video.php page. The link ends up looking like this:
http://thedomainname/videos/?t=theterm
And then the code on the archives-videos.php page looks like this:
The Archives page
and below that I can list all of the videos in that particular taxonomy-term:
So that gives me a universal page that will show whatever video-type I send to it..which allows the website owner to create whatever new video-type they want, which will automatically display on the home-page as a link.
Again, I think someone with a better working knowledge of PHP could write this in a better way (which I would love to see) but the idea is there and working.
After searching the internet for some time I’ve finally got it working.
This goes in the functions.php
This goes into the category.php template
This code will display sub categories if a category has one, if it doesn’t have a sub category it will display post for that category, in my case products. If a category doesn’t have any sub categories and doesn’t have any posts it won’t display anything.