1

我正在尝试应用 template_include 过滤器来从我的插件中扩充模板。我已经看到很多人成功使用了以下内容:

function include_template_files($template_file) {
  global $post;
  $plugindir = dirname( __FILE__ );
  if ('mycustomposttype' == get_post_type()){
    $templatefilename = 'mytemplate.php';
    $template = $plugindir . '/theme_files/' . $templatefilename;
    return $template;
  }
  return $template_file;
}
add_filter( 'template_include', 'include_template_files' );

get_post_type() 返回空,并且 $post 没有为我的自定义帖子实例化。这适用于 WP 类型(帖子、页面等)。我究竟做错了什么?

我在 WP 3.7.1 上使用默认的 211 主题。

更新

我以这种方式注册我的类型:

function register_mycustom_post_type() {
        register_post_type( 'mycustomposttype', array(
            'labels' => array(
                'name' => 'My posts',
                'singular_name' => 'My post',
                'menu_name' => 'My posts',
                'add_new' => 'New custom post',
                'add_new_item' => 'Add new custom post',
            ),
            'public' => true,
            'show_ui' => true,
            'menu_icon' => plugins_url('myicon.png',__FILE__),
            'supports' => array( 'title' ,'thumbnail', 'editor' ),
        ) );
    }
add_action('init','register_mycustom_post_type');

第二次更新(部分解决方案):

问题是由永久链接重写给出的。如果我使用默认 URL (index.php?...) 它工作正常。

解决方案

为我的帖子类型添加正确的重写选项解决了这个问题:

'rewrite' => array(
                'slug' => 'mytype',
                'with_front' => false
),
4

3 回答 3

0

尝试像这样使用get_queried_object()函数

$queried = get_queried_object();
print_r($queried);

您应该从当前页面获取和反对信息

于 2013-11-14T20:28:00.000 回答
0

我自己回答。问题是永久链接重写。将以下内容添加到 register_post_type 选项解决了该问题:

'rewrite' => array(
            'slug' => 'mytype',
            'with_front' => false
),
于 2013-11-15T20:57:46.117 回答
0

有时这也可以工作:

get_query_var( 'post_type' );
于 2015-04-24T20:07:40.343 回答