LOST in WP Template Hireachy

The question:

I am trying to create a Custom Theme which is supposed to list and dialysis Custom Post Types First Base on Taxonomy and In next page List and Display Them based on Taxonomy Term which linked from the First Page.
so what i have is
enter image description here

based on what I have I decides to create 5 page custom template using WordPress like

Template Name: Movies

Then I moved them pages into Navbar.
For each page I hard coded a custom loop by Wp-Query to get the list of Custom Post Type under the Page Taxonomy. For Example For Movie Page I wrote a Query to list all movieCPTs under movieTax and for sidebar a query to list all terms for that taxonomy. Well This is not obviously the standard method to do in WP and now I am having trouble to link the result of each terms click in next page, Again for example in following image structure if a user clicks on “B” I do not know how to show the result.

enter image description here

I studied the codes a lot and saw some tutorial online for WP template hierarchy but I couldn’t figure it out how to use them in a case like this! Can you please take a time and let me know what standard hierarchy do I need to present a template like this dynamically?

I really appreciate your time in advance

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

The template hierarchy does not provide for archive index pages. This is an issue that was raised through a trac ticket, but that idea was scrapped and will never be implemented due to different theme stuctures across the board

So the following will never exists natively

  • Pages that lists all posts from a given taxonomy

  • Pages that lists posts from all custom post types

  • Pages that displays an index of terms belonging to a taxonomy (same goes for categories, tags and author pages)

The following do exists in the hierarchy

  • Pages that displays all posts from a given term from a custom taxonomy (see taxonomy pages in the template hierarchy)

  • Pages that displays all posts from a given custom post type (see archive pages in the template hierarchy)

With this all said, you can re-evalute your structures. Your terms can be displayed on a taxonomy.php template, taxonomy-{$taxonomy}.php template or taxonomy-{$taxonomy}-{$term}.php template. For this, simply copy your index.php and rename accordingly. To link to the separate term pages, use get_term_link to link to your term pages.

You will need to create a custom page.php template to display all terms under a given taxonomy.

Just for interest sake, I have done a post a while ago about a dynamic page template for custom post types and for categories. How this work, you have only one template respectively. When creating a new page in the backend, you can choose which custom post type or category respectively you need to display on the specific page. So instead of four or five page templates, you will have only one. Either of these two templates can be modified to your liking for taxonomies as well. Here are the two posts


From comments

So in my case I am going to have a page called movie-page.php and a taxonomy.php and finally taxonomy-movieTax.php ? right? Just one final question? Do I need to have same structure for archive.php, as well?

Looking at your structure, I would say yes. Just a tip, if you name your page template, call it page-movie.php not the other way around. As I stated in my previous comment

Only if you need a completely different setup for each term, which I don’t think you will do. A single taxonomy.php will work perfectly if you need the same layout for all terms across all four taxonomies. But again, if you need each taxonomy to have a different layout, you can create four different taxonomy-{$taxonomy}.php templates. This is really all up to you and your layout. The hierarchy just simply provides if you need a different layout between taxonomies or terms in a given taxonomy

Archive pages would be different though. Archive pages are used to display your custom post type posts. This archive page will list all posts from your cpt, regardless of taxonomies or terms. You only have one option here apart from the default archive.php, and that is archive-{$post_type}.php

But again, you don’t need to create these custom pages. An archive.php and a taxonomy.php would be more than enough if you do not need any fancy layout differences or very specific needs per custom post type, taxonomy or terms.

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment