I have a plugin which contains a single PHP template for both single_template
and archive_template
for multiple post types. I am loading a client side application in this template, which is why I only need a single template for all four variants.
The code I have is:
add_filter( 'single_template', 'react_template' );
add_filter( 'archive_template', 'react_template' );
function react_template( $template ) {
global $post;
if ( $post->post_type == 'tutorial' || $post->post_type == 'dashboard' ) {
if ( file_exists( PLUGIN_DIR . 'templates/learning.php' ) ) {
return PLUGIN_DIR . 'templates/learning.php';
}
}
return $template;
}
This is working perfectly for my needs. However, if no posts exist for either tutorial
or dashboard
types I get an error because $post
is null.
It's not a big deal as in production there will always be numerous posts in the system, but it bugs me. I do not want to have a named template for each type. Is there a way to have my archive template work when no posts exist?
I have a plugin which contains a single PHP template for both single_template
and archive_template
for multiple post types. I am loading a client side application in this template, which is why I only need a single template for all four variants.
The code I have is:
add_filter( 'single_template', 'react_template' );
add_filter( 'archive_template', 'react_template' );
function react_template( $template ) {
global $post;
if ( $post->post_type == 'tutorial' || $post->post_type == 'dashboard' ) {
if ( file_exists( PLUGIN_DIR . 'templates/learning.php' ) ) {
return PLUGIN_DIR . 'templates/learning.php';
}
}
return $template;
}
This is working perfectly for my needs. However, if no posts exist for either tutorial
or dashboard
types I get an error because $post
is null.
It's not a big deal as in production there will always be numerous posts in the system, but it bugs me. I do not want to have a named template for each type. Is there a way to have my archive template work when no posts exist?
One way to accomplish this may be to use the pre_get_posts
to conditionally add the single_template
and archive_template
filters.
namespace StackExchange\WordPress;
/**
* Add filters for `single_template`, and `archive_template` for the tutorial and
* dashboard archives.
*/
function pre_get_posts( $query )
{
if( is_singular() )
{
$post_type = get_post_type();
if ( 'tutorial' === $post_type || 'dashboard' === $post_type) )
{
add_filter( 'single_template', __NAMESPACE__ . '\react_template' );
return $query;
}
}
if ( ! is_post_type_archive( [ 'tutorial', 'dashboard' ] ) )
{
return $query;
}
if( ! isset( $query->query[ 'post_type' ] )
{
return $query;
}
$post_type = $query->query['post_type'];
if ( 'tutorial' === $post_type || 'dashboard' === $post_type) )
{
add_filter( 'archive_template', __NAMESPACE__ . '\react_template' );
}
return $query;
}
add_action( 'pre_get_posts', __NAMESPACE__ . '\pre_get_posts' );
/**
* Return the React Template, if it exists
*/
function react_template( $template )
{
if ( file_exists( PLUGIN_DIR . 'templates/learning.php' ) )
{
return PLUGIN_DIR . 'templates/learning.php';
}
return $template;
}