Here’s an example that “abuses” the tag description field to store a template name for the tag. We use the tag_template
filter to get the tag description if it exists and include a template file with the description as part of the name. You could extend this to whitelist specific template names so typos don’t result in trying to load a file that doesn’t exist:
function wpd_tag_template( $templates="" ){
$tag = get_queried_object();
if( !empty( $tag->description ) ){
$templates = locate_template( 'custom-tag-' . $tag->description . '.php', false );
}
return $templates;
}
add_filter( 'tag_template', 'wpd_tag_template' );
So for example the description contains template-1
, WordPress will load the file custom-tag-template-1.php
for this tag.