【问题标题】:Trying to add a custom post type page template from plugin into Sage 10 theme尝试将自定义帖子类型页面模板从插件添加到 Sage 10 主题
【发布时间】: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 页面仍然可以工作。

【问题讨论】:

    标签: php wordpress laravel-blade roots-sage


    【解决方案1】:

    你可以使用single_template过滤钩子。

    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;});
    

    【讨论】:

    • 谢谢,但我所说的代码不起作用
    • 抱歉,在template_redirect 钩子或init 钩子下试试这个过滤器。不起作用的原因是我认为您的插件在 single_template 钩子或过滤器之后加载
    • 是的,我使用 Sage 10,即使我将 single-speaker.php 文件放在主题根目录中,它也不起作用,因为它迫使我使用 content-signle-speaker.blade .php 格式。我会尝试重定向挂钩谢谢
    • 不幸的是,他们都没有工作。 D:感谢您的宝贵时间
    【解决方案2】:

    试试这个:

    add_filter('template_include', function ($template) {
        global $post;
    
        if ( ! empty($post->post_type) && 'speaker' === $post->post_type) {
            if (file_exists(SPEAKER_PLUGIN_URL.'/templates/single-speaker.php')) {
                $template = SPEAKER_PLUGIN_URL.'/templates/single-speaker.php';
            }
        }
    
        return $template;
    }, PHP_INT_MAX);
    

    提示: 确认您的常量 SPEAKER_PLUGIN_URL 实际上是您所期望的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多