I have a multisite network with some shared custom post types, and I'm trying to create a function that loads a custom single-{cpt}
template for a given post type by $blog_id
when defined, and otherwise fall-back to the default single-
template. In this case, the custom post type is resource
, which would default to single-resource.php
and I'm setting a variable $custom_resource_single
to the applicable template file name as follows:
if ($blog_id == 5) {
$custom_resource_single = 'single-resource-alt.php';
}
I'm then trying to pass this variable to a function used as a callback in the template_include
filter hook as follows (adapting somewhat from this thread), but it's not working:
function custom_resource_single($custom_resource_single, $template) {
if ( is_singular('resource') && !empty($custom_resource_single) ) {
$custom = locate_template($custom_resource_single);
return $custom;
}
return $template;
}
add_filter( 'template_include', function() use ($custom_resource_single, $template) { custom_resource_single($custom_resource_single, $template); });
I attempting to use a closure based on some other threads I've found here, but I'm clearly not using it properly. I can successfully achieve the result by passing an anonymous function with the $blog_id
conditional directly to template_include
hook, but I'd really like to be able to pass a variable ($custom_resource_single
) to the function that is defined elsewhere.
Please let me know if my aim as described above is unclear, and thanks in advance for any assistance.
use ( &$custom_resource_single )
? But why don't you run the conditional from within thetemplate_include
filter - e.g.if ( 5 == get_current_blog_id() ) $template = 'single-resource-alt.php';
?multisite-functions.php
in which I've set up a bunch of options grouped by$blod_id
to try to keep everything organized, so my hope is to to set the$custom_resource_single
variable by$blog_id
and then pass that variable to the function I described above, if that makes sense- I could do what you're suggesting, but I'd like to keep the conditional out of the filter function itself. I've been able to do so for other things like enabling custom post types, taxonomies, etc. but can't get it working for the single- template