首页 文章

Wordpress插件自定义帖子类型单页

提问于
浏览
0

我正在开发一个创建自定义帖子类型的Wordpress插件 . 该自定义帖子类型将需要它自己的single.php页面 . 我知道我可以在我的主题中创建一个名为single- .php的文件,但我需要将此文件放在插件目录中 . 如何让Wordpress认识到我想使用插件目录中的single-posttype.php而不是我的主题目录?

1 回答

  • 0

    这是我使用的,只需将 dirname(__FILE__) .'/templates/ 替换为您拥有的任何目录结构 . 关于这一点的好处是,如果您在 $file 位置没有"override"文件,它将默认为正确的主题文件 .

    add_filter( 'single_template', 'override_single_template' );
    function override_single_template( $single_template ){
        global $post;
    
        $file = dirname(__FILE__) .'/templates/single-'. $post->post_type .'.php';
    
        if( file_exists( $file ) ) $single_template = $file;
    
        return $single_template;
    }
    

    当然,你也可以这样做

    archive_template
    

    $file = dirname(__FILE__) .'/templates/archive-'. $post->post_type .'.php';
    

相关问题