使用自定义功能重新注册自定义帖子类型

时间:2014-02-27 作者:Kamal Joshi

我已经创建了如下自定义帖子类型。

register_post_type( \'connector\',
    array(
        \'labels\' => array(
            \'name\' => __( \'Connectors\' ),
            \'singular_name\' => __( \'Connector\' ),
            \'all_items\' => __( \'All Connectors\' ),
            \'add_new\' => __( \'Add Connector\' ),
            \'add_new_item\' => __( \'Add New Connector\' ),
            \'edit\' => __( \'Edit\'),
            \'edit_item\' => __( \'Edit Connector\' ),
            \'new_item\' => __( \'New Connector\'),
            \'view\' => __( \'View Connector\'),
            \'view_item\' => __( \'View Connector\'),
            \'search_items\' => __( \'Search Connectors\'),
            \'not_found\' => __( \'No Connectors found\' ),
            \'not_found_in_trash\' => __( \'No Connectors found in trash\' ),
            \'parent\' => __( \'Parent Connector\')
        ),      

    \'description\' => __( \'This is where you can add new Connectors\' ),
    \'public\' => true,
    \'show_ui\' => true,
    \'capability_type\' => \'page\',        
    \'publicly_queryable\' => true,
    \'exclude_from_search\' => false,
    \'hierarchical\' => true,
    \'rewrite\' => array( \'slug\' => \'connector\', \'with_front\' => false ),
    \'query_var\' => true,
    \'supports\' => array( \'title\', \'editor\', \'thumbnail\', \'comments\', \'excerpt\',\'author\',/*, \'page-attributes\'*/ ),
    \'taxonomies\' => array(\'post_tag\'), // this is IMPORTANT for adding tags
    \'has_archive\' => true,
    \'show_in_nav_menus\' => true,
    \'show_in_menu\'=>false
    )
);
然后进一步应用到我的网站上,并拥有大量的作者。

现在,我想将自定义功能添加到我的自定义帖子类型-“connector”。我遵循了Change custom post type to hierarchical after being registered

function modify_connectors() {
    if ( post_type_exists( \'connector\' ) ) {


$capabilities = array(
        \'publish_posts\' => \'publish_connectors\',
        \'edit_posts\' => \'edit_connectors\',
        \'edit_others_posts\' => \'edit_others_connectors\',
        \'delete_posts\' => \'delete_connectors\',
        \'delete_others_posts\' => \'delete_others_connectors\',
        \'read_private_posts\' => \'read_private_connectors\',
        \'edit_post\' => \'edit_connector\',
        \'delete_post\' => \'delete_connector\',
        \'read_post\' => \'read_connector\',
    );


    global $wp_post_types, $wp_rewrite;
    $wp_post_types[\'connector\']->hierarchical = true;
    $wp_post_types[\'connector\']->capability_type = \'connector\';
    $wp_post_types[\'connector\']->capabilities = $capabilities;
    //I am not sure from nowonwards am I doing right or wrong
    $args = $wp_post_types[\'connector\'];
    $wp_rewrite->add_rewrite_tag("%connector%", \'(.+?)\', $args->query_var ? "{$args->query_var}=" : "post_type=connector&name=");
    add_post_type_support(\'connector\',\'page-attributes\');
    }
}
add_action( \'init\', \'modify_connectors\', 1 );
我在“用户角色编辑器”菜单下找不到与连接器相关的任何自定义功能。这是否可以在注册post类型完成后添加自定义功能?

1 个回复
SO网友:Brad Dalton

你有没有试过这样的方法:

\'description\' => __( \'This is where you can add new Connectors\' ),
\'public\' => true,
\'show_ui\' => true,
\'capability_type\' => \'page\',        
\'publicly_queryable\' => true,
\'exclude_from_search\' => false,
\'hierarchical\' => true,
\'capabilities\'        => array(
\'publish_posts\'       => \'update_core\',
\'edit_others_posts\'   => \'update_core\',
\'delete_posts\'        => \'update_core\',
\'delete_others_posts\' => \'update_core\',
\'read_private_posts\'  => \'update_core\',
\'edit_post\'           => \'edit_posts\',
\'delete_post\'         => \'update_core\',
\'read_post\'           => \'edit_posts\',
),
\'rewrite\' => array( \'slug\' => \'connector\', \'with_front\' => false ),
\'query_var\' => true,
\'supports\' => array( \'title\', \'editor\', \'thumbnail\', \'comments\', \'excerpt\',\'author\',/*, \'page-attributes\'*/ ),
\'taxonomies\' => array(\'post_tag\'), // this is IMPORTANT for adding tags
\'has_archive\' => true,
\'show_in_nav_menus\' => true,
\'show_in_menu\'=>false
));

结束

相关推荐

PHP致命错误:无法为wp-includes/capabilities.php中的非对象调用重载函数

我在apache日志中遇到了太多以下错误。PHP Fatal error: Cannot call overloaded function for non-object in wp-includes/capabilities.php on line 1187这是函数current\\u user\\u can($capability)的内部,第1187行如下所示:$current_user = wp_get_current_user(); 我不知道问题出在哪里?