自定义发布类型-公文包不再起作用

时间:2016-11-04 作者:user53340

我有一个用于公文包的自定义帖子类型,以前它很有用,现在它不再显示在管理菜单中。但是,只有在查看网站时,它才会显示在管理栏中,而不会显示在仪表板中。如果我点击去公文包部分添加一个项目,我会得到作弊信息,嗯?错误消息。我真的希望有人能帮我解决这个问题,因为我花了几个小时试图解决这个问题。提前感谢!

<?php
function register_portfolio() {

    $labels = array( 
        \'name\' => __( \'Add New Portfolio\', \'bk\' ),
        \'singular_name\' => __( \'Portfolio\', \'bk\' ),
        \'add_new\' => __( \'Add New\', \'bk\' ),
        \'add_new_item\' => __( \'Add New Portfolio Item\', \'bk\' ),
        \'edit_item\' => __( \'Edit Portfolio\', \'bk\' ),
        \'new_item\' => __( \'New Portfolio\', \'bk\' ),
        \'view_item\' => __( \'View Portfolio\', \'bk\' ),
        \'search_items\' => __( \'Search Portfolio\', \'bk\' ),
        \'not_found\' => __( \'No Portfolio found\', \'bk\' ),
        \'not_found_in_trash\' => __( \'No Portfolio found in Trash\', \'bk\' ),
        \'parent_item_colon\' => __( \'Parent Portfolio:\', \'bk\' ),
        \'menu_name\' => __( \'Portfolio\', \'bk\' ),
    );
    $args = array( 
        \'labels\' => $labels,
        \'hierarchical\' => true,
        \'supports\' => array(\'title\', \'editor\', \'thumbnail\'),
        \'public\' => true,
        \'show_ui\' => true,
        \'show_in_menu\' => true,
        \'show_in_nav_menus\' => true,
        \'publicly_queryable\' => true,
        \'exclude_from_search\' => false,
        \'has_archive\' => true,
        \'rewrite\' => array(\'slug\' => \'portfolio\'),

        \'query_var\' => true,
        \'can_export\' => true,
        \'map_meta_cap\' => true,
        \'capabilities\' => array(
            \'edit_post\' => \'edit_portfolio\',
            \'edit_posts\' => \'edit_portfolio\',
            \'edit_others_posts\' => \'edit_other_portfolio\',
            \'publish_posts\' => \'publish_portfolio\',
            \'edit_publish_posts\' => \'edit_publish_portfolio\',
            \'read_post\' => \'read_portfolio\',
            \'read_private_posts\' => \'read_private_portfolio\',
            \'delete_post\' => \'delete_portfolio\',
            \'delete_posts\' => \'delete_portfolio\',
            \'delete_others_posts\' => \'delete_others_portfolio\',
            \'delete_published_posts\' => \'delete_published_portfolio\'
        ),
        \'capability_type\' => array(\'portfolio\', \'portfolio\'),
    );
    register_post_type( \'bk-portfolio\', $args );
}
add_action( \'init\', \'register_portfolio\' );

// Add custom taxonomies
function portfolio_create_taxonomies() {    

    $portfolio_type_labels = array(
        \'name\' => __( \'Portfolio Categories\', \'taxonomy general name\', \'bk\' ),
        \'singular_name\' => __( \'Portfolio Category\', \'taxonomy singular name\', \'bk\' ),
        \'search_items\' =>  __( \'Search Portfolio Categories\', \'bk\' ),
        \'all_items\' => __( \'All Portfolio categories\', \'bk\' ),
        \'most_used_items\' => null,
        \'parent_item\' => null,
        \'parent_item_colon\' => null,
        \'edit_item\' => __( \'Edit Portfolio Category\', \'bk\' ),
        \'update_item\' => __( \'Update Portfolio Category\', \'bk\' ),
        \'add_new_item\' => __( \'Add New Portfolio Category\', \'bk\' ),
        \'new_item_name\' => __( \'New Portfolio Category\', \'bk\' ),
        \'menu_name\' => __( \'Categories\', \'bk\' ),
    );
    register_taxonomy(\'portfolio-category\', \'bk-portfolio\',array(
        \'hierarchical\' => true,
        \'labels\' => $portfolio_type_labels,
        \'show_ui\' => true,
        \'query_var\' => true,
        \'rewrite\' => array(\'slug\' => \'portfolio-category\' )
    ));


}
add_action( \'init\', \'portfolio_create_taxonomies\', 0 );


function manage_portfolio_capabilities() {
    // gets the role to add capabilities to
    $admin = get_role( \'administrator\' );

    // replicate all the remapped capabilites from the custom post type portfolio
    $caps = array(
        \'edit_portfolio\',
        \'edit_portfolio\',
        \'edit_other_portfolio\',
        \'publish_portfolio\',
        \'edit_published_portfolio\',
        \'read_portfolio\',
        \'read_private_portfolio\',
        \'delete_portfolio\',
        \'delete_portfolio\',
        \'delete_others_portfolio\',
        \'delete_published_portfolio\'
    );
    // give all the capabilities to the administrator
    foreach ($caps as $cap) {
        $admin->add_cap( $cap );
    }

}
add_action( \'admin_init\', \'manage_portfolio_capabilities\');

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

删除map\\u meta\\u cap。它将出现在您的仪表板中

    \'map_meta_cap\' => true,  //remove this line
为什么要限制所有这些功能?看起来你还没有给自己能力去看有限制的帖子。。。

如果你不知道为什么会有这些代码,你可能不需要这些代码:(在之前保存一份插件的副本,以防万一,因为可能有另一个函数出于某种原因需要它。例如,你在代码末尾有一个函数,它也在查看功能。)

    \'map_meta_cap\' => true,
    \'capabilities\' => array(
        \'edit_post\' => \'edit_portfolio\',
        \'edit_posts\' => \'edit_portfolio\',
        \'edit_others_posts\' => \'edit_other_portfolio\',
        \'publish_posts\' => \'publish_portfolio\',
        \'edit_publish_posts\' => \'edit_publish_portfolio\',
        \'read_post\' => \'read_portfolio\',
        \'read_private_posts\' => \'read_private_portfolio\',
        \'delete_post\' => \'delete_portfolio\',
        \'delete_posts\' => \'delete_portfolio\',
        \'delete_others_posts\' => \'delete_others_portfolio\',
        \'delete_published_posts\' => \'delete_published_portfolio\'
    ),
    \'capability_type\' => array(\'portfolio\', \'portfolio\'),
此外,我还是名称空间的大力支持者,以减少未来的冲突。将有很多人和主题使用“公文包”作为他们的cpt。

您的公文包名称已经确定(bk\\U公文包而不仅仅是公文包),但您的实际职能没有确定。

function register_portfolio() {
应该是

function bk_register_portfolio() {
您需要确保通过更改

add_action( \'init\', \'register_portfolio\' );

add_action( \'init\', \'bk_register_portfolio\' );
最后一点。

在标签数组中,您将您的投资组合命名为“添加新投资组合”,这没有意义。“名称”标签也应为复数。将其更改为公文包。然后您可以将您的添加新行更改为“添加新投资组合”。。。(好吧,那只是吹毛求疵)

$labels = array( 
    \'name\' => __( \'Add New Portfolio\', \'bk\' ),  //change to Portfolios
    \'singular_name\' => __( \'Portfolio\', \'bk\' ),
    \'add_new\' => __( \'Add New\', \'bk\' ),  //Change to Add New Portfolio
    \'add_new_item\' => __( \'Add New Portfolio Item\', \'bk\' ),
    \'edit_item\' => __( \'Edit Portfolio\', \'bk\' ),
    \'new_item\' => __( \'New Portfolio\', \'bk\' ),
    \'view_item\' => __( \'View Portfolio\', \'bk\' ),
    \'search_items\' => __( \'Search Portfolio\', \'bk\' ),
    \'not_found\' => __( \'No Portfolio found\', \'bk\' ),
    \'not_found_in_trash\' => __( \'No Portfolio found in Trash\', \'bk\' ),
    \'parent_item_colon\' => __( \'Parent Portfolio:\', \'bk\' ),
    \'menu_name\' => __( \'Portfolio\', \'bk\' ),
);

相关推荐

在将代码添加到函数后无法登录WordPress wp-admin。php

我在函数末尾添加以下代码。php文件,用于根据自定义帖子的帖子标题填充分类法。问题是,当我添加代码时,尝试登录wp admin时会出现以下错误。非常感谢您能帮助我们弄清楚为什么会发生这种情况。Error:错误:由于意外输出,Cookie被阻止。有关帮助,请参阅此文档或尝试支持论坛。Code: <?php function update_custom_terms($post_id) { // only update terms if