Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Wordpress寄存器\u post\u类型无效的post类型_Php_Wordpress - Fatal编程技术网

Php Wordpress寄存器\u post\u类型无效的post类型

Php Wordpress寄存器\u post\u类型无效的post类型,php,wordpress,Php,Wordpress,我正在wordpress中创建我自己的帖子,但是我遇到了一个小问题,我不知道如何解决它 下面的寄存器\u post\u类型创建了无效的post类型错误 add_action( 'init', 'create_post_type' ); function create_post_type() { register_post_type( 'talent', array( 'labels' => array( 'name' => __( 'T

我正在wordpress中创建我自己的帖子,但是我遇到了一个小问题,我不知道如何解决它

下面的寄存器\u post\u类型创建了无效的post类型错误

add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'talent',
    array(
        'labels' => array(
            'name' => __( 'Talent' ),
            'singular_name' => __( 'talent' )
        ),
    'public' => true,
    'has_archive' => true,
    )
);
}

它似乎不喜欢人才这个词。如果我将“天赋”改为“阿里斯特”,它会起作用。然而,它需要是“人才”的网址。我查看了wordpress,使用talent不会与默认的wordpress设置产生任何冲突。

这确实很奇怪,我尝试了你的代码片段,它按预期工作。这让我觉得你可能已经尝试通过另一种方法注册相同的帖子类型了。它可以通过plugin,也可以在同一个function.php文件中,或者使用插件在数据库上创建自定义类型(例如CPT UI)

我建议你们两个:

  • 追踪问题的本质,以防它不会给你带来其他麻烦。你将来可能会遇到另一个类似的问题,这肯定会浪费你宝贵的时间
  • 避免使用通用名称空间,特别是使用全局共享的东西,如自定义帖子类型,我通常称它们为
    projectname\u postname
    ,因此,例如,您将以以下内容结束:
    stackoverflow\u talent
假设您选择第二种方式,只需对特定的post类型使用slug rewrite:

register_post_type( 'myproject_talent',
    array(
        'labels' => array(
            'name' => __( 'Talent' ),
            'singular_name' => __( 'talent' )
        ),
    'public' => true,
    'has_archive' => true,
    'rewrite' => array('slug' => 'talent'),
    )
);
现在,您的所有帖子都将显示
/talent/
路径。 请记住在创建post类型后刷新permalink结构,否则在前端无法看到它


希望解决了你的问题。

我也有同样的问题,但答案对我来说并不适用。如果其他人发现了此问题,请确保post类型名称不超过20个字符。这可能就是问题所在,Wordpress不想让你知道


积分转到:

确保帖子类型名称不超过20个字符:stackoverflow.com/a/26029803/722036