为什么邮政类型模板不适合我?

时间:2014-07-01 16:20:24

标签: wordpress

所以,according to this page,它声明:

  

单{post_type} .PHP
      如果您的自定义帖子类型是' product'和/或query_var =" product",WordPress会查找single-product.php来显示   单一或固定的帖子。

所以我在我的functions.php中有这个代码:

register_post_type( 'project',$args);

我有这个模板:single-project.php

那么为什么我的自定义帖子不是使用此模板?

更新
使用get_post_types()我可以看到我的帖子类型未列出。如果没有注册,那么为什么我可以创建和删除自定义帖子?

add_action('after_setup_theme','init_setup');

function init_setup() {
  add_action( 'init', 'create_post_type' );
}

function create_post_type() {
  $args = array(
    'labels' => array(
      'name' => __( 'Prosjekter' ),
      'singular_name' => __( 'Prosjekt' ),
      'add_new' => __('Nytt prosjekt')
    ),
    'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
    'has_archive' => false,
    'taxonomies' => array('category'),
    'menu_position' => 5,
    'supports' => array( 'title', 'editor', 'thumbnail', 'page-attributes','post_tag' ),
    'rewrite' => array('slug' => 'prosjekt')
  );

  register_post_type( 'project',$args);
}

1 个答案:

答案 0 :(得分:1)

来自食典委:

  

register_post_type只能通过'init'动作调用。   如果在'init'之前调用它,那么它将无法工作   如果稍后调用,帖子类型将无法正常工作。

您是否尝试过这样注册?

add_action( 'init', 'create_post_type' ); // NOT inside `init_setup()`

<强> [编辑]

我能想到的另一件事是,因为你使用rewrite,你可能想要刷新重写规则。同样来自食典委:

  

注意:如果在插件中注册帖子类型,请致电   激活和取消激活挂钩中的flush_rewrite_rules()(参见   下面激活刷新重写)。如果flush_rewrite_rules()不是   使用,那么你将不得不手动转到设置&gt;永久链接和   在自定义帖子类型之前刷新永久链接结构   显示正确的结构。

如果您此时不想使用flush_rewrite_rules()功能,只需进入设置&gt;永久链接并重新保存永久链接首选项。