Use pre-existing base template for taxonomy archive and custom post type

Hi all,

(I’m using Sage 8 on a legacy site).

I have (what I think is) a unique issue / use-case:

I have a Custom Post Type hub_resource and a Taxonomy called hub_topic.

I want both of these to use a base template I’ve defined, called base-template-hub.php.

Doing this for the hub_resource CPT is easy enough; I just have a template-hub.php file with this in the header:

<?php
/**
 * Template Name: Hub
 * Template Post Type: hub_resource
 */
?>

But trying to get the hub_topic Taxonomy to use this same base template is seemingly impossible.

Does anybody have any ideas?

Cheers,
Lloyd

FIXED:

  • Created base-taxonomy-hub_topic.php
  • Filled it with the following:
<?php
include 'base-template-hub.php';

Thanks to @bokorir

Hey @joneslloyd - a couple of ideas:

First, you could step back and do conditional login in your base.php file to check if the current request is for your CPT or taxonomy and load the correct wrapper template if true.

Second, the SageWrapping class (which returns the appropriate wrapper) applies a filter prior to returning the wrapper template. I haven’t tried it, but it looks like you could hook into that.

From wrapper.php:

public function __toString() {
  $this->templates = apply_filters('sage/wrap_' . $this->slug, $this->templates);
  return locate_template($this->templates);
}

So maybe something like (untested):

function hub_taxonomy_wrapper($templates) {
  if (is_tax('hub_topic')) {
    array_unshift($templates, 'base-template-hub.php');
  }
  return $templates;
}
add_filter('sage/wrap_base', __NAMESPACE__ . '\\hub_taxonomy_wrapper');

Hey @mmirus – Thanks for this! Will give it a go.

That works really well :slight_smile:

/**
 * Force hub_topic taxonomy to use the base-template-hub.php template
 */
function hub_taxonomy_wrapper($templates)
{
    if (is_tax('hub_topic')) {
        array_unshift($templates, 'layouts/base-template-hub.php');
    }
    return $templates;
}
add_filter('sage/wrap_base', __NAMESPACE__ . '\\hub_taxonomy_wrapper');
1 Like