最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom post types - Creating a non-hierarchical Taxonomy that behaves like categories

programmeradmin1浏览0评论

I've created a custom post-type along with a custom taxonomy and everything is fine, apart from one particular thing.

First off, my use case: I need it to act like a category system, however not be hierarchical (one level only), so I left both to the default setting of false for the hierarchical setting.

This has been fine, apart from one problem where it allows me to add a "sub-level" within the metabox.

First, the code:

register_post_type( 'letter',
                    array(
                        'labels' => array(
                            'name' => __( 'Letters' ),
                            'singular_name' => __( 'Letter' ),
                            //..........
                        ),
                        'public' => true,
                        'taxonomies' => array('letter'),
                        //........
                    )
);

// Our args for the custom taxonomy below
$args = array(
    'labels' => array(
        'name' => __('Recipients'),
        'singular_name' => __('Recipient'),
        //.....
    ),
    'meta_box_cb' => 'post_categories_meta_box',
);

// Register a custom taxonomy for our letter categories
register_taxonomy( 'recipient', 'letter', $args );

// Connect the post type and taxonomy together to be safe
register_taxonomy_for_object_type( 'recipient', 'letter' );

Now, as you can see I needed to set the meta_box_cb to post_categories_meta_box otherwise it would end up having the functionality of the tag metabox, so, now it looks like this:

So this is good, I get the category functionality that I like, however I also get the "Add New Recipient" where when you open it up it allows you to add a new "recipient" and select a parent recipient; this I do not want.

Is my only option to create a new callback function based on the post_categories_meta_box function and remove the respective code to get rid of that parent functionality?

I don't particularly want to do that as then it would break future updates to this function.

Is there another option?

I've created a custom post-type along with a custom taxonomy and everything is fine, apart from one particular thing.

First off, my use case: I need it to act like a category system, however not be hierarchical (one level only), so I left both to the default setting of false for the hierarchical setting.

This has been fine, apart from one problem where it allows me to add a "sub-level" within the metabox.

First, the code:

register_post_type( 'letter',
                    array(
                        'labels' => array(
                            'name' => __( 'Letters' ),
                            'singular_name' => __( 'Letter' ),
                            //..........
                        ),
                        'public' => true,
                        'taxonomies' => array('letter'),
                        //........
                    )
);

// Our args for the custom taxonomy below
$args = array(
    'labels' => array(
        'name' => __('Recipients'),
        'singular_name' => __('Recipient'),
        //.....
    ),
    'meta_box_cb' => 'post_categories_meta_box',
);

// Register a custom taxonomy for our letter categories
register_taxonomy( 'recipient', 'letter', $args );

// Connect the post type and taxonomy together to be safe
register_taxonomy_for_object_type( 'recipient', 'letter' );

Now, as you can see I needed to set the meta_box_cb to post_categories_meta_box otherwise it would end up having the functionality of the tag metabox, so, now it looks like this:

So this is good, I get the category functionality that I like, however I also get the "Add New Recipient" where when you open it up it allows you to add a new "recipient" and select a parent recipient; this I do not want.

Is my only option to create a new callback function based on the post_categories_meta_box function and remove the respective code to get rid of that parent functionality?

I don't particularly want to do that as then it would break future updates to this function.

Is there another option?

Share Improve this question asked Aug 12, 2018 at 13:46 BrettBrett 4145 silver badges25 bronze badges 2
  • Well, another dirty option would be using CSS to hide that field or remove that element using jQuery. – Abhik Commented Aug 12, 2018 at 13:50
  • @Abhik Hmm true, I guess if it comes down to it that may prove to be a better, yet dirty, option. – Brett Commented Aug 12, 2018 at 13:57
Add a comment  | 

2 Answers 2

Reset to default 2

This blog post by "Gazchap" deals with exactly the situation you are, and they updated it after publishing to your follow-up problem:

Fortunately, version 4.4 of WordPress introduced a filter – post_edit_category_parent_dropdown_args – that could be used to control the parent terms shown in these meta boxes. It’s designed to let the developer change the terms listed, for example excluding certain categories, or only showing “top level” parent terms and not their descendants. There is no control that is designed to stop the menu being shown at all, but there is one that allows us to trick WordPress into hiding the parent drop-down select.

Here’s the filter that you need:

add_filter( 'post_edit_category_parent_dropdown_args', 'hide_parent_dropdown_select' );

function hide_parent_dropdown_select( $args ) {
    if ( 'YOUR_TAXONOMY_SLUG' == $args['taxonomy'] ) {
        $args['echo'] = false;
    }
    return $args;
}

Why does this work? The echo argument is set to true by default, and makes WordPress echo the dropdown select into the meta box. By setting this to false, WordPress instead returns the HTML instead, so it doesn’t get rendered to the browser.

Note that of course YOUR_TAXONOMY_SLUG should be replaced with the slug of your custom taxonomy.

Anyone attempting this hack should definitely read the blog post in full, it also has some other useful tips.

Given that it's a dirty option you could try this way:

function dirty_admin_customization() {

    // #newtaxonomy_parent
    $style = '<style type="text/css">#newrecipient_parent { display: none !important; }</style>';

    echo $style . "\n";

}
add_action( 'admin_head-post.php',     'dirty_admin_customization' );
add_action( 'admin_head-post-new.php', 'dirty_admin_customization' );
发布评论

评论列表(0)

  1. 暂无评论