admin管理员组文章数量:1420973
I'm trying to apply a filter for a custom post type and give it a different template. All from within a plugin. However, despite confirming that add_hooks()
is called (everything else works in that area), the filter is flatly ignored.
I can only guess that I got something wrong as it looks like the filter is never applied. I know it is not because I put an else/die
in there. It should either give me my template or die (right?) but it ignores me. How do I do this correctly?
class some_cool_new_stuff{
public function __construct(){
$this->add_hooks();
}
public function page_template( $page_template ) {
if ( is_page( 'my_lovely_post_type' ) ) {
$page_template = MY_CUSTOM_CONST_FOR_PLUGIN_DIR . 'templates/business-page.php'; // this does not happen but the path is 100% correct
}else{
die("Help me, I'm going to die!!"); // this never happens
}
return $page_template;
}
public function add_hooks(){
// ...
add_action( 'init', array($this,'awesome_function') );
add_filter( 'page_template', array($this,'page_template') );
// ...
}
// ...
}
Here is some other stuff that doesn't work for me:
add_filter( 'my_lovely_post_type_template', array($this,'page_template') );
add_filter( 'template_include', array($this,'page_template') );
I'm trying to apply a filter for a custom post type and give it a different template. All from within a plugin. However, despite confirming that add_hooks()
is called (everything else works in that area), the filter is flatly ignored.
I can only guess that I got something wrong as it looks like the filter is never applied. I know it is not because I put an else/die
in there. It should either give me my template or die (right?) but it ignores me. How do I do this correctly?
class some_cool_new_stuff{
public function __construct(){
$this->add_hooks();
}
public function page_template( $page_template ) {
if ( is_page( 'my_lovely_post_type' ) ) {
$page_template = MY_CUSTOM_CONST_FOR_PLUGIN_DIR . 'templates/business-page.php'; // this does not happen but the path is 100% correct
}else{
die("Help me, I'm going to die!!"); // this never happens
}
return $page_template;
}
public function add_hooks(){
// ...
add_action( 'init', array($this,'awesome_function') );
add_filter( 'page_template', array($this,'page_template') );
// ...
}
// ...
}
Here is some other stuff that doesn't work for me:
add_filter( 'my_lovely_post_type_template', array($this,'page_template') );
add_filter( 'template_include', array($this,'page_template') );
Share
Improve this question
edited Jul 7, 2019 at 15:41
Matthew Brown aka Lord Matt
asked Jul 7, 2019 at 15:35
Matthew Brown aka Lord MattMatthew Brown aka Lord Matt
1,0683 gold badges13 silver badges34 bronze badges
1 Answer
Reset to default 0After trying a whole bunch of filter possibilities, I found one that worked.
add_filter( 'single_template', array($this,'page_template') );
Which got my function to be called. However, I also needed to make some more changes (not least of which taking out the die
line). This was, finally, what worked:
public function page_template( $page_template ) {
global $post;
if ( 'my_lovely_post_type' === $post->post_type ) {
$page_template = BUSINESS_PAGE_PLUGIN_DIR . 'templates/business-page.php';
}
return $page_template;
}
本文标签: How do I use a plugin to swap out the template file for a custom post type
版权声明:本文标题:How do I use a plugin to swap out the template file for a custom post type? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745339178a2654180.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论