【问题标题】:Wordpress plugin custom post type single pageWordpress 插件自定义帖子类型单页
【发布时间】:2018-05-10 22:48:49
【问题描述】:

我正在开发一个创建自定义帖子类型的 Wordpress 插件。该自定义帖子类型将需要它自己的 single.php 页面。我知道我可以在我的主题中创建一个名为 single-{custom post type}.php 的文件,但我需要这个文件位于插件目录本身中。如何让 Wordpress 识别出我想使用插件目录中的 single-posttype.php 而不是主题目录?

【问题讨论】:

标签: php wordpress


【解决方案1】:

这就是我使用的,只需将dirname(__FILE__) .'/templates/ 替换为您拥有的任何目录结构即可。这样做的好处是,如果您在 $file 位置没有“覆盖”文件,它将默认使用正确的主题文件。

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

当然你也可以用 with 做同样的事情

archive_template

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

【讨论】:

  • 像魅力一样工作。谢谢楼主!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-15
  • 2011-03-20
  • 2014-05-21
  • 2018-06-15
  • 1970-01-01
  • 2017-02-06
相关资源
最近更新 更多