Template and Controller naming for custom taxonomy

Using Sage9 beta 4 I’m getting lost on the naming of templates files, controller function etc.
could someone point me in the right direction please?

inside of resources/views/ I have a custom taxonomy for a custom post type, called: film_director

my template is named: taxonomy-film_director.blade.php
it is used by wordpress for a single term

my class is called Taxonomy_film_director but the variable it should inject is not found on the page above; i get a “Notice: Undefined variable: director_name”

bonus question: I can’t figure out the name of the template file for the taxonomy archive (the page where to list all the terms within the custom taxonomy)

The controller file name needed to match the template file name (that’s what was breaking it).
While the class name can’t have a dash in there, so I used an underscore instead.

Happy to read your thoughts on the bonus question :slight_smile: — is there any other neater way than creating a page with a custom template?

As far as I know, WordPress doesn’t have a page that lists terms in a taxonomy, only pages that list all the posts in a taxonomy term. i.e. if you have the taxonomy cakes with the terms angel and devils-food, the template taxonomy-cakes.php will be used to display the archives of posts that have the terms in that taxonomy applied to them, i.e. /cakes/devils-food/. So no, I don’t believe there’s a neater way than creating a custom page template, unless you want to try fiddling around with routing and template selection to point WordPress at a particular template that way.

1 Like

I’m curious about what you @alwaysblank would use to fiddle with routing and templates? :slight_smile:
I tried Cortex but couldn’t get it to be displayed through Blade in the latest Sage9 (cf: Sage9 template & wrapper ) despite Ben’s kind sharing of the book example on how to render from php. I also tried with rareloop/wp-router but seems out of date as well and has been integrated into their Lumbersjack.

What would be the best way to have some custom routing for Wordpress?

If I really wanted to get serious with routing I would probably not use WordPress. :wink: When I have needed to do stuff that involves messing with paths and routing-things, I’ve just used WP’s Rewrites API: https://codex.wordpress.org/Rewrites_API It’s a bit obtuse sometimes, but it’s pretty powerful and you’re using the same mechanism WP uses internally.

1 Like

So, small update to this: I actually found myself just yesterday needing to do some significantly complicated routing. I tried doing it with the Rewrites API and it was a bit of a nightmare, so on the suggestion of @Log1x I tried Cortex, and it works pretty much out of the box! I was able to replicate the problem you ran into (Sage9 Template & Wrapper) but normal template selection seems to work fine (i.e. don’t specify a template in your route, just let WordPress handle it.

So I’d tentatively revise my answer to be that Cortex is probably an easier solution to a lot of problems than Rewrites API. :wink:

1 Like

This is brilliant! I’m a bit concerned by lowering globally the minimum stability requirement; would you know if it’s something that can be specific to the package (not sure where to look)?

Thanks so much for coming back and updating your answer; I stupidly lost myself in the complexity of a custom class and controlling the Blade template and forgot to come back to basics. Good lesson! Thanks!

I appreciate this thread is pretty old, but it caught my eye when looking for a little help on another area of this topic, so I thought I’d chime in with my tuppence.

I may have the wrong end of the stick, but if you want a template for a specific term, you can do so. For example:

taxonomy: cakes
term: angel
template: taxonomy-cakes-angel.blade.php
controller: taxonomy-cakes-angel.php

This is something I’m currently implementing, so I know it works (not for cakes).

Now for what I came here for: Here’s what’s left me scratching my head.

My recollection is that OP wanted a page that displayed a list of all terms in a taxonomy, not a list of all items associated with a term. You’re definitely correct that you can template the page of a specific term, but I don’t believe WordPress has a built-in template that will display all terms in a taxonomy.

I actually had to revisit this, and manage to get it to work by removing
remove_all_filters('template_include');
from Cortex/brainwp ResultHandler.php in the add_filter function handling ‘template_include’

then build a custom template file in ./views called awards.blade.php with the Blade basic calls ( @extends(‘layouts.app’) @section(‘content’) and @endsection) and my own logic and queries

However the queried objects from the custom routes won’t follow through (once passed through Blade, the query becomes a standard page query); unless I had a custom query in the template it won’t get access to the custom query results.
SO roughly it seems so far to be a convoluted way of having a page with custom template/logic, with the only main davantage to lock down issues of users editing things in the admin. Not yet that DRY.

edit: nb: in ./site/composer.json I used ‘dev-refactoring-fastroute’ instead of 1.0.alpha-X