Custom post types often need to be accompanied with custom taxonomies.
In many scenarios the archive pages for the custom post type and the custom taxonomy (or multiple) should be the same.
The code snippet sections of this site is a good example of this. The custom post type archive (/code/) and the custom taxonomy archives are the same (/code/tagged/term-slug) are identical.
Using the template_include
filter (which is very useful for other things) I can easily force specific templates to be used when specific conditionals are met.
In the example below I load the code custom post type archive template if the user is on the custom taxonomy. This allows me to maintain a single template file that is used in multiple places.
<?php | |
/** | |
* Redirect taxonomy to use custom post type archive template | |
* | |
* @author Jared Atchison | |
* @link http://jaredatchison.com/code/ | |
* @param string $template | |
* @return string | |
*/ | |
function ja_template_redirect( $template ) { | |
if ( is_tax( 'code-snippet-tag' ) ) { | |
$template = get_query_template( 'archive-code-snippet' ); | |
} | |
return $template; | |
} | |
add_filter( 'template_include', 'ja_template_redirect' ); |