admin管理员组文章数量:1130349
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?
Share Improve this question asked Nov 28, 2018 at 0:10 hsimahhsimah 1054 bronze badges1 Answer
Reset to default 2One 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;
}
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?
Share Improve this question asked Nov 28, 2018 at 0:10 hsimahhsimah 1054 bronze badges1 Answer
Reset to default 2One 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;
}
本文标签: pluginsarchivetemplate override when no posts exist
版权声明:本文标题:plugins - archive_template override when no posts exist 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749146935a2323153.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论