自定义帖子类型不在管理菜单中

时间:2016-06-20 作者:PreFiXAUT

当我为我的网站创建一个私有和自定义主题时,我为我想要显示的不同类型的数据创建了自定义帖子类型(并使用自定义字段添加额外数据)。

不管怎么说,由于我设法让一切都正常工作,不知何故,这些项目从管理菜单中消失了,因此我无法为我的自定义类型编辑或添加新内容。

以下是pastebin上的来源:http://pastebin.com/dmA6PQSR

if (!function_exists("pre_posttype")):
function pre_posttype() {
    $types = array(
        "pre_selection" => array(
            "single" => "Selection",
            "description" => "A Selection of items which will appear on the front-page",
            "public" => false,
            "publicly_queryable" => true,
            "show_ui" => true,
            "show_in_nav_menus" => false,
            "show_in_menu" => true,
            "show_in_admin_bar" => false,
            "menu_position" => 30,
            "menu_icon" => "dashicons-visibility",
            "hierachical" => false,
            "supports" => array("title", "editor")
        ),
        "pre_project" => array(
            "single" => "Project",
            "description" => "(Programming-) Projects which this Website shows",
            "public" => true,
            "show_ui" => true,
            "show_in_nav_menus" => true,
            "show_in_menu" => true,
            "show_in_admin_bar" => true,
            "menu_position" => 31,
            "menu_icon" => "dashicons-category",
            "hierachical" => false,
            "supports" => array("title", "editor", "thumbnail")
        ),
        "pre_template" => array(
            "single" => "Template",
            "description" => "Template-Project for streaming",
            "public" => true,
            "show_ui" => true,
            "show_in_nav_menus" => true,
            "show_in_menu" => true,
            "show_in_admin_bar" => true,
            "menu_position" => 32,
            "hierachical" => false,
            "supports" => array("title", "editor")
        )
    );

    foreach ($types as $name => $t) {
        # Saving SINGLE and MULTI in variables and unsetting them, to avoid problems since we merge and post it
        # When neither is set, we\'ll create them from the name
        $s = isset($t["single"]) ? $t["single"] : (ucfirst(strtolower(strpos($name, "_") ? substr(strpos($name, "_")+1) : $name)));
        $m = isset($t["multi"]) ? $t["multi"] : (strcasecmp($s{strlen($s)-1},"s") ? $s : "{$s}s");
        # Un-setting extra values so they won\'t show up on the wp-registering and may screw shit up.
        unset($t["multi"], $t["single"]);

        register_post_type($name, PreLib::merge_all($t, array(
            # Defaults which will be applied when nothing is specified
            "label" => $m,
            "labels" => array(
                "name"                  => $m,
                "singular_name"         => $s,
                "add_new"               => "Add New",
                "add_new_item"          => "Add new {$s}",
                "edit_item"             => "Edit {$s}",
                "new_item"              => "New {$s}",
                "view_item"             => "View {$s}",
                "search_items"          => "Search {$s}",
                "not_found"             => "Nothing Found",
                "not_found_in_trash"    => "Nothing found in Trash",
                "parent_item_colon"     => "Parent {$s}",
                "all_items"             => "All {$m}",
                "archives"              => "{$s} Archives",
                "insert_into_item"      => "Insert into {$s}",
                "uploaded_to_this_item" => "Uploaded to this {$s}",
                "menu_name"             => $m,
                "update_item"           => "Update {$s}",
            ),
            "capability_type" => array(
                strtolower($s),
                strtolower($m)
            ),
            "rewrite" => array(
                "slug" => strtolower($s)
            ),
            "query_var" => $s,
            "rest_base" => strtolower($s)
        )));
    }
}
endif;
add_action("init", "pre_posttype");

public static function merge_all() {
       # If there are less than 2 arrays we can\'t merge
       if (func_num_args()  2) {
           # Iterate through all subs and call this method one after another
           foreach ($sub as $s) $main = merge_all($main, $s);
           return $main;
       }

       foreach ($sub as $key => $val) {
           # Main has same val as sub, next
           if ($main[$key] == $val) continue;
           # Main doesn\'t have the key, so set it
           if (!isset($main[$key])) {
               $main[$key] = $sub[$key];
               continue;
           }
           # Merge all sub-arrays as well
           if (is_array($val) && is_array($main[$key])) $main[$key] = merge_all($main[$key], $val);
       }

       return $main;
   }
帖子类型正在注册并在这个意义上起作用,它只是没有出现在管理菜单或管理栏中。它确实通过使用查询和菜单来显示。

我希望有人能帮忙,因为我已经一个多月没找到bug了-\\u-提前谢谢

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

您的能力类型无效。如果可以的话,我会删除下面的代码。WordPress自动生成capability\\u类型。

        "capability_type" => array(
            strtolower($s),
            strtolower($m)
        ),
查看有关capability\\u type的文档。https://codex.wordpress.org/Function_Reference/register_post_type