使用插件的分类模板

Taxonomy template using plugin

我创建了一个 wordpress 插件,它在激活时创建了 2 个自定义 post 类型和分类法。

分类名称是 "product"。

我创建了一个文件"taxonomy-product.php"

<?php
/**
 * The template for displaying Product Archieve
 */

 get_header(); ?>
    <div class="wrap">

    <?php if ( have_posts() ) : ?>
        <header class="page-header">
                        </header><!-- .page-header -->
    <?php endif; ?>

    <div id="primary" class="content-area">
        <main id="main" class="site-main" role="main">
//Query for pulling posts
    <?php
$args = array(
  'post_type'   => 'product_reviews',
  'post_status' => 'publish',   
 );

$products = new WP_Query( $args );
if( $products->have_posts() ) :
?>

<?php get_footer();

此模板包含在插件文件夹中,当调用产品分类法时,模板会显示,但会抛出致命错误无法找到 get_header()

有人可以帮助我哪里出错以及如何使用插件包含自定义分类模板。

下面是我用来在插件文件中包含自定义模板的代码:

 add_filter('template_include', 'set_template');
function set_template( $template ){

       if( is_tax('product'))
        $template = plugin_dir_url(__FILE__ ).'templates/taxonomy-product.php';

    return $template;
}

这类似于在 woo commerce 中包含产品模板。

您应该按路径而不是 URL 包含文件,因此更改

$template = plugin_dir_url(__FILE__ ).'templates/taxonomy-product.php';

$template = plugin_dir_path(__FILE__ ).'templates/taxonomy-product.php';