I have several custom post types that share archive and single templates but being different from the usual single post.
The WordPress way would be to create one archive and single php for each CPT with the same content. But which would be the roots DRY way? Should I modify base.php? Or should I do some kind of
It’s a judgement call. For minor changes between templates, I would use a conditional. For major changes I would use a new template.
In your case I would probably create archive-custom.php and filter template_include to get the CPTs to use it. With the singles I would probably just edit single.php and load the custom content via get_template_part.
I would definitely tidy up your logic and remove it from the templates though, so that it can be reused.
The point is that base file is the same one (same heading, logo, footer, etc, but the changing part is the content so: Can I create a function that selects a custom-archive-for-several-cpt.php instead of index.php or archive.php, having just one base.php???
Nevermind, (I leave my previous paragraph just in case it might help others). I just realized that with template_include we are modifying what we are calling from base.php include roots_template_path();, right? So we have just one base.php.
Please, could you tell me if this is the right way, including the filter call, in /lib/custom.php
Perfect, I assume the rest is correct right?
The return $template and add_filter('template_include', 'custom_cpt', 98); is the part I had doubts about.
I’m doing a very similar thing. Which is to render the same template file for the singular view of a few different post types. It works fine, however, the Show Current Template plugin thinks single.blade.php template is being used instead. It all works fine, but it’s just a little confusing. Am I doing this right?