未注册的两种自定义发布类型之一

时间:2015-09-08 作者:Joe Ng\'ethe

我有这个功能,添加到自定义的帖子类型,这是非常简单和直截了当的,如下所示;

function create_hr_post_type() {

  register_post_type( \'informationtechnology\',
    array(
      \'labels\' => array(
        \'name\' => __( \'Information Technology\' ),
        \'singular_name\' => __( \'Information Technology\' )
      ),
      \'public\' => true,
      \'has_archive\' => true,
      \'rewrite\' => array(\'slug\' => \'informationtechnology\'),
    )
  );

  register_post_type( \'humanresource\',
    array(
      \'labels\' => array(
        \'name\' => __( \'HR\' ),
        \'singular_name\' => __( \'HR\' )
      ),
      \'public\' => true,
      \'has_archive\' => true,
      \'rewrite\' => array(\'slug\' => \'humanresource\'),
    )
  );


}

add_action( \'init\', \'create_hr_post_type\' );
然而,在检查我的管理左栏时,只注册了一种帖子类型。我只看到HR职位类型!这是怎么回事?我错过了什么?

我尝试重新排序代码,使IT类型排在第一位,但仍然不起作用。

1 个回复
SO网友:birgire

来自Codex:

但是,请务必密切注意自定义post类型标识符不能超过20个字符,因为数据库中的post\\u类型列当前是该长度的VARCHAR字段。

请退房:

echo strlen( \'informationtechnology\' );
如果你有WP_DEBUG 激活后,您将看到此警告:

register_post_type 调用不正确。帖子类型名称的长度必须介于1到20个字符之间。有关详细信息,请参阅WordPress中的调试。(此消息是在版本4.2中添加的。)

因此,这是一个很好的提醒,在开发网站时使用它;-)

本部分register_post_type() 负责此检查,我在第一次浏览函数时错过了它,但在运行@Joeng\'ethe的示例时确认了它:

if ( empty( $post_type ) || strlen( $post_type ) > 20 ) {
    _doing_it_wrong(
        __FUNCTION__, 
        __( \'Post type names must be between 1 and 20 characters in length.\' ), 
       \'4.2\' 
    );
    return new WP_Error( 
        \'post_type_length_invalid\',
        __( \'Post type names must be between 1 and 20 characters in length.\' ) 
    );
}
编辑查看$post_type 注册帖子类型时的参数

$post_type

(字符串)(必需)立柱类型。(max. 20 characters, cannot contain capital letters or spaces)

默认值:无

相关推荐