尝试将自定义帖子类型页面模板从插件添加到 Sage 10 主题

分享于2022年07月17日 laravel-blade php roots-sage wordpress 问答
【问题标题】:尝试将自定义帖子类型页面模板从插件添加到 Sage 10 主题(Trying to add a custom post type page template from plugin into Sage 10 theme)
【发布时间】:2022-06-30 05:54:59
【问题描述】:

正如标题所说,我正在创建一个 Sage 10 主题(我们不使用基岩)。这个网站需要一个自定义的“speaker”帖子类型,它还带有一个single-speaker.php页面来显示信息。所有这些功能都是在插件中编写的,但我无法让页面模板填充到主题中。

自定义帖子类型和元框确实有效,我也可以获取值。但是,single-speaker.php 页面将不起作用。我试过了:

add_filter('single_template', 'speaker_single_template');

function speaker_single_template($single) {

    global $post;

    /* Checks for single template by post type */
    if ( $post->post_type == 'speaker' ) {
        if ( file_exists( SPEAKER_PLUGIN_URL . '/templates/single-speaker.php' ) ) {
            return SPEAKER_PLUGIN_URL . '/templates/single-speaker.php';
        }
    }

    return $single;

}

我原以为这个过滤器会将模板页面推送到主题中,但事实并非如此。

Sage 使用刀片指令是否存在问题?我假设默认的 php 页面仍然可以工作。


【解决方案1】:

You can use single_template filter hook.

add_filter('single_template', function ($single) {
global $post;
/* Checks for single template by post type */
if ( $post->post_type == 'POST TYPE NAME' ) {
    if ( file_exists( PLUGIN_PATH . '/Custom_File.php' ) ) {
        return PLUGIN_PATH . '/Custom_File.php';
    }
}
return $single;});
  • Thank you, but that is the code I have that I said does not work
  • Sorry for that, try this filter under template_redirect hook or init hook. The reason for not working is that I think your plugin load after the single_template hook or filter
  • Yeah, where I am using Sage 10 even when I put the the single-speaker.php file in the theme root it will not work because it forces me to use the content-signle-speaker.blade.php format. I will try the redirect hook thanks
  • unfortunately neither of them worked. D: Thank you for your time