将其他所有分类作为元框添加到自定义帖子类型

时间:2018-05-02 作者:xxtesaxx

我有一个自定义的帖子类型,它带有一个类别分类法。这种分类法在侧面显示为一个元框。到目前为止,还很基本。

现在,我想在“正常”位置的内容下方添加额外的元框。我想为系统中的每个公共分类显示一个元框。到目前为止,我使用此代码添加框:

public function add_meta_boxes_rfe_taxonomy_related__premium_only($post)
{
    $taxonomies = get_taxonomies([\'public\' => true], \'objects\');
    foreach ($taxonomies as $taxonomy) {

        if (\'rfe_taxonomy_related_category\' === $taxonomy->name) {
            continue;
        }

        $taxonomy_name = $taxonomy->labels->name;
        $taxonomy_slug = $taxonomy->name;
        $taxonomy_is_hierarchical = $taxonomy->hierarchical;
        $object_type = $taxonomy->object_type;

        $post_type_names = [];

        foreach ($object_type as $post_type) {
            $post_type_name = isset($post_type_names[$post_type]) ? $post_type_names[$post_type] : false;

            if (!$post_type_name) {
                $post_type_name = get_post_type_object($post_type)->label;
                $post_type_names[$post_type] = $post_type_name;
            }

            if ($taxonomy_is_hierarchical) {
                add_meta_box(
                    \'rfe_taxonomy_related_meta_box_\' . $taxonomy_slug, //ID
                    $taxonomy_name . \' (\' . $post_type_name . \')\', //Title
                    [$this, \'build_hierarchical_taxonomy_meta_box__premium_only\'], //Callback
                    \'rfe_taxonomy_related\', //Post type to add to
                    \'normal\',
                    \'low\'
                );
            } else {
                add_meta_box(
                    \'rfe_taxonomy_related_meta_box_\' . $taxonomy_slug, //ID
                    $taxonomy_name . \' (\' . $post_type_name . \')\', //Title
                    [$this, \'build_flat_taxonomy_meta_box__premium_only\'], //Callback
                    \'rfe_taxonomy_related\', //Post type to add to
                    \'normal\',
                    \'low\'
                );
            }
        }

    }
}

public function build_hierarchical_taxonomy_meta_box__premium_only($post, $metabox)
{
    $taxonomy_slug = substr($metabox[\'id\'], strlen(\'rfe_taxonomy_related_meta_box_\'));
    $box[\'args\'][\'taxonomy\'] = $taxonomy_slug;
    post_categories_meta_box($post, $box);
}

public function build_flat_taxonomy_meta_box__premium_only($post, $metabox)
{
    $taxonomy_slug = substr($metabox[\'id\'], strlen(\'rfe_taxonomy_related_meta_box_\'));
    $box[\'args\'][\'taxonomy\'] = $taxonomy_slug;
    post_tags_meta_box($post, $box);
}
这已经为我提供了元框,但用于平面分类法的元框似乎不起作用。我无法添加新标签或从最流行的标签中进行选择。

第二个问题是,当我保存帖子时,所有更改都会丢失。我想知道我错过了什么,或者我做错了什么。

enter image description here

1 个回复
SO网友:xxtesaxx

根据LWS Mo的建议,这可以通过使用register_taxonomy_for_object_type() 为系统中的所有分类法注册自定义帖子类型,然后使用remove_meta_box()add_meta_box 将其重新添加到新位置。我不确定我是否真的使用了正确的钩子-但是,嘿,它是有效的:)

值得一提的是,所有分类法都以子菜单的形式出现在我的帖子类型下,因此我还必须删除子菜单条目。

首先添加挂钩:

add_action(\'init\', [$this, \'create_taxonomy_related_post_type__premium_only\']);
add_action(\'wp_loaded\', [$this, \'add_meta_boxes_rfe_taxonomy_related__premium_only\']);
add_action(\'admin_init\', [$this, \'rename_taxonomy_meta_boxes_on_rfe_taxonomy_related__premium_only\']);
add_action(\'admin_menu\', [$this, \'remove_taxonomy_menu_from_rfe_taxonomy_related__premium_only\']);
然后添加具有自己分类法的自定义帖子类型:

public function create_taxonomy_related_post_type__premium_only()
{
    register_post_type(\'rfe_taxonomy_related\',
        array(
            \'labels\' => array(
                \'name\' => \'Taxonomy Related\',
                \'singular_name\' => \'Taxonomy Related\',
                \'add_new\' => \'Add New\',
                \'add_new_item\' => \'Add New Layout\',
                \'edit\' => \'Edit\',
                \'edit_item\' => \'Edit Layout\',
                \'new_item\' => \'New Layout\',
                \'view\' => \'View\',
                \'view_item\' => \'View Layout\',
                \'search_items\' => \'Search Layouts\',
                \'not_found\' => \'No Layouts found\',
                \'not_found_in_trash\' => \'No Layouts found in Trash\',
            ),
            \'public\' => true,
            \'exclude_from_search\' => true,
            \'publicly_queryable\' => false,
            \'supports\' => array(\'title\', \'editor\'),
            \'taxonomies\' => array(\'rfe_taxonomy_related_category\'),
            \'menu_icon\' => \'dashicons-format-aside\',
            \'has_archive\' => false,
        )
    );

    register_taxonomy(
        \'rfe_taxonomy_related_category\',
        array(\'rfe_taxonomy_related\'),
        array(
            \'hierarchical\' => true,
            \'labels\' => array(
                \'name\' => __(\'Categories\', \'jt-revolution-for-elementor\'),
                \'singular_name\' => __(\'Category\', \'jt-revolution-for-elementor\'),
                \'search_items\' => __(\'Search Category\', \'jt-revolution-for-elementor\'),
                \'all_items\' => __(\'All Categories\', \'jt-revolution-for-elementor\'),
                \'parent_item\' => __(\'Parent Category\', \'jt-revolution-for-elementor\'),
                \'parent_item_colon\' => __(\'Parent Category:\', \'jt-revolution-for-elementor\'),
                \'edit_item\' => __(\'Edit Category\', \'jt-revolution-for-elementor\'),
                \'update_item\' => __(\'Update Category\', \'jt-revolution-for-elementor\'),
                \'add_new_item\' => __(\'Add New Category\', \'jt-revolution-for-elementor\'),
                \'new_item_name\' => __(\'New Category Name\', \'jt-revolution-for-elementor\'),
                \'menu_name\' => __(\'Categories\', \'jt-revolution-for-elementor\'),
            ),
            \'show_ui\' => true,
            \'show_admin_column\' => true,
            \'query_var\' => true,
        )
    );
}
下次使用register_taxonomy_for_object_type 要将系统中的每个分类添加到我的帖子类型中,请执行以下操作:

public function add_meta_boxes_rfe_taxonomy_related__premium_only($post)
{
    $args = array(\'public\' => true);
    $output = \'objects\';
    $taxonomies = get_taxonomies($args, $output);
    foreach ($taxonomies as $taxonomy) {
        if (\'rfe_taxonomy_related_category\' === $taxonomy->name) {
            continue;
        }
        register_taxonomy_for_object_type($taxonomy->name, \'rfe_taxonomy_related\');
    }
}
现在,我可以使用remove_meta_boxadd_meta_box:

public function rename_taxonomy_meta_boxes_on_rfe_taxonomy_related__premium_only()
{
    //Stores display names of post types so we can cache them
    $post_type_names = [];

    $taxonomies = get_taxonomies([\'public\' => true], \'objects\');

    foreach ($taxonomies as $taxonomy) {

        //Skip our own taxonomy
        if (\'rfe_taxonomy_related_category\' === $taxonomy->name) {
            continue;
        }

        $taxonomy_name = $taxonomy->labels->name;
        $taxonomy_slug = $taxonomy->name;
        $taxonomy_post_types = [];

        $object_type = $taxonomy->object_type;
        foreach ($object_type as $post_type) {

            //Skip our own post type
            if (\'rfe_taxonomy_related\' === $post_type) {
                continue;
            }

            $post_type_name = isset($post_type_names[$post_type]) ? $post_type_names[$post_type] : false;

            if (!$post_type_name) {
                $post_type_name = get_post_type_object($post_type)->label;
                $post_type_names[$post_type] = $post_type_name;
            }

            $taxonomy_post_types[] = $post_type_name;
        }

        if ($taxonomy->hierarchical) {
            $meta_box_id = $taxonomy_slug . \'div\';
            $meta_box_callback = \'post_categories_meta_box\';
        } else {
            $meta_box_id = \'tagsdiv-\' . $taxonomy_slug;
            $meta_box_callback = \'post_tags_meta_box\';
        }

        remove_meta_box($meta_box_id, \'rfe_taxonomy_related\', \'side\');

        add_meta_box(
            $meta_box_id,
            $taxonomy_name . \' (\' . implode(\', \', $taxonomy_post_types) . \')\',
            $meta_box_callback,
            \'rfe_taxonomy_related\',
            \'normal\',
            \'low\',
            [\'taxonomy\' => $taxonomy_slug]
        );

    }

}
最后隐藏分类法的子菜单页,除了我自己添加到帖子类型中的自定义页面:

public function remove_taxonomy_menu_from_rfe_taxonomy_related__premium_only()
{
    global $submenu;

    //Get all the taxonomies of which we need to remove the submenu
    $taxonomies = [];
    foreach (get_taxonomies([\'public\' => true], \'objects\') as $taxonomy) {
        if (\'rfe_taxonomy_related_category\' === $taxonomy->name) {
            continue;
        }

        $taxonomies[] = $taxonomy->name;
    }

    $post_type = \'rfe_taxonomy_related\';
    if (isset($submenu[\'edit.php?post_type=\' . $post_type])) {
        foreach ($submenu[\'edit.php?post_type=\' . $post_type] as $k => $sub) {
            $should_remove = false;
            foreach ($taxonomies as $taxonomy) {
                if (strpos($sub[2], \'taxonomy=\' . $taxonomy)) {
                    $should_remove = true;
                    break;
                }
            }
            if ($should_remove) {
                unset($submenu[\'edit.php?post_type=\' . $post_type][$k]);
            }
        }
    }
}
如果你有改进代码的建议,我很乐意听听!

结束

相关推荐

查找自定义分类Metabox的回调函数

我正在寻找一种方法,将我的自定义分类元盒(右侧)移动到帖子区域(中间)。我知道你可以简单地拖放它,但我希望它在新用户的默认张贴区。我的方法是使用remove_meta_box(), 然后使用add_meta_box(). 问题是I don\'t know what callback function to call when adding it back./* Remove movies metabox from sidepanel */ function hide_metabox(){