自定义帖子类型不起作用

时间:2013-04-25 作者:Justin Helgerson

我刚刚建立了一个测试环境来测试我的新Wordpress网站。我没有迁移数据库;我再次进行了自己的安装,并将我的主题(这是一个简单的主题)移植到我的新实例。除了我正在使用的一种自定义帖子类型外,其他一切都很好。

以下是来自的相关代码functions.php:

function createCommunityPostType() {
    $args = array(
            \'public\' => true,
            \'label\' => \'Community\',
            \'capability_type\' => \'community\',
                \'capabilities\' => array(
                                \'edit_post\' => \'edit_community\',
                                \'edit_private_posts\' => \'edit_private_communities\',
                                \'edit_published_posts\' => \'edit_published_communities\',
                                \'edit_posts\' => \'edit_communities\',
                                \'edit_others_posts\' => \'edit_others_communities\',

                                \'delete_post\' => \'delete_community\',
                                \'delete_posts\' => \'delete_communities\',
                                \'delete_others_posts\' => \'delete_others_communities\',
                                \'delete_private_posts\' => \'delete_private_communities\',
                                \'delete_published_posts\' => \'delete_published_communities\',

                                \'read_private_posts\' => \'read_private_communities\',
                                \'read_post\' => \'read_community\',
                                \'publish_posts\' => \'publish_communities\'
                ),
                \'map_meta_cap\' => true,
                \'has_archive\' => true
    );
    register_post_type(\'community\', $args );
}
add_action(\'init\', \'createCommunityPostType\');
我的自定义帖子类型没有显示在菜单中,我可以看到这些功能不存在(我正在使用members插件使其更加可见)。

我知道代码正在执行,因为我添加了一些print 调用之前和之后的语句register_post_type. 打印语句是HTML响应的一部分。

我不明白为什么这种帖子不起作用。我有其他方法functions.php 处理样式表、脚本、菜单和侧栏的一些替代等。这些都很好用。

Edit:

我改为使用默认功能,但菜单选项仍然没有出现:

function createCommunityPostType() {
    $args = array(
            \'public\' => true,
            \'label\' => \'Community\',
            \'capability_type\' => \'community\',
                \'capabilities\' => array(
                                \'edit_post\' => \'edit_posts\',
                                \'edit_private_posts\' => \'edit_posts\',
                                \'edit_published_posts\' => \'edit_posts\',
                                \'edit_posts\' => \'edit_posts\',
                                \'edit_others_posts\' => \'edit_posts\',

                                \'delete_post\' => \'delete_posts\',
                                \'delete_posts\' => \'delete_posts\',
                                \'delete_others_posts\' => \'delete_posts\',
                                \'delete_private_posts\' => \'delete_posts\',
                                \'delete_published_posts\' => \'delete_posts\',

                                \'read_private_posts\' => \'read\',
                                \'read_post\' => \'read\',
                                \'publish_posts\' => \'publish_posts\'
                ),
            \'map_meta_cap\' => true,
                \'has_archive\' => true
    );
    register_post_type(\'community\', $args );
}
add_action(\'init\', \'createCommunityPostType\');
然而,我切换到使用Wordpress中一些非常基本的示例代码,出现了自定义帖子类型(CPT):

function codex_custom_init() {
    $args = array( \'public\' => true, \'label\' => \'Books\' );
    register_post_type( \'book\', $args );
}
add_action( \'init\', \'codex_custom_init\' );

1 个回复
最合适的回答,由SO网友:Chip Bennett 整理而成

一个问题是\'capability_type\' 参数\'community\' 与您的\'capabilities\' 数组值。

From the Codex:

capability\\u类型

(字符串或数组)(可选)用于构建读取、编辑和删除功能的字符串。May be passed as an array to allow for alternative plurals when using this argument as a base to construct the capabilities, e.g. array(\'story\', \'stories\'). 默认情况下,capability\\u类型用作构建功能的基础。看来map_meta_cap 需要设置为true,才能使其正常工作。

您的能力类型是:

\'capability_type\' => \'community\'
并注意您的\'capabilities\' 阵列:

\'capabilities\' => array(
    \'edit_post\' => \'edit_community\',
    \'edit_private_posts\' => \'edit_private_communities\',
    \'edit_published_posts\' => \'edit_published_communities\',
    \'edit_posts\' => \'edit_communities\',
    \'edit_others_posts\' => \'edit_others_communities\',

    \'delete_post\' => \'delete_community\',
    \'delete_posts\' => \'delete_communities\',
    \'delete_others_posts\' => \'delete_others_communities\',
    \'delete_private_posts\' => \'delete_private_communities\',
    \'delete_published_posts\' => \'delete_published_communities\',

    \'read_private_posts\' => \'read_private_communities\',
    \'read_post\' => \'read_community\',
    \'publish_posts\' => \'publish_communities\'
),
您的能力类型为singular, 您构建的能力是plural.

尝试将功能类型更改为:

\'capability_type\' => array( \'community\', \'communities\' )
此外,如果您定义\'capability_type\', 您不需要明确定义每个功能。所以,试试看omitting the \'capabilities\' argument 完全

如果在此之后您的CPT没有出现,则问题可能是由于您的成员插件配置:

确保已正确定义自定义功能确保当前用户具有CPT中引用的功能

结束

相关推荐

从admin-ajax.php调用admin-ajax.php

我正在编写一个插件,它调用另一个插件的ajax的输出,设置在wp_ajax_我会直接调用这些函数,但插件中的限制使其成为非选项。要明确的是,发生了这样的事情:用户选择一个动作,然后点击apply,我的插件函数在admin ajax中调用。php(通过wp_ajax_ 钩子)插件函数创建一个非编码url,如下所示:$url = wp_nonce_url( admin_url( \'admin-ajax.php?action=plugin_action&post_id=\' . $post_id),