admin管理员组

文章数量:1418134

I have several sites with custom post types and taxonomies registered by different plugins. Is there a way to add or change a parameter like this:

'rewrite' => array( 'hierarchical' => true )

by adding something to functions.php—without completely re-registering the type/taxonomy? I want to continue to use certain plugins to manage types and taxonomies, but not all parameters are provided.

I have several sites with custom post types and taxonomies registered by different plugins. Is there a way to add or change a parameter like this:

'rewrite' => array( 'hierarchical' => true )

by adding something to functions.php—without completely re-registering the type/taxonomy? I want to continue to use certain plugins to manage types and taxonomies, but not all parameters are provided.

Share Improve this question edited Jul 31, 2019 at 14:54 supertrue asked May 17, 2011 at 6:12 supertruesupertrue 3,01610 gold badges46 silver badges60 bronze badges 2
  • 1 A custom post type and taxonomy is registered on every page call, so it should be no problem to, for example, remove it from the plugin and do the registration completely from your own code. It is also possible just copy the rewrite rules that the hierarchical parameter will generate and add them yourself. Finally, maybe the plugin has some hooks that allow to change the output? What plugins do you use? (Use @Jan when you reply in a comment and I get a notification) – Jan Fabry Commented May 17, 2011 at 15:45
  • @Jan I have used at least ten different CPT/taxonomy plugins in search of one that does not have bugs or unsupported parameters... I really liked More Types and More Taxonomies, but they had some problems so I'm now using Custom Press. How would I add the rewrite rules? Would that be easier or preferable to somehow hooking into/filtering the custom taxonomy registration? – supertrue Commented May 17, 2011 at 21:02
Add a comment  | 

3 Answers 3

Reset to default 2

you could modify the global variable $wp_post_types or in the case of taxonomies, $wp_taxonomies

for example, this will change the menu name and all the labels for the default Posts to Bacon:

function change_post_menu_label() {
    global $menu;
    global $submenu;
    $menu[5][0] = 'Bacon';
    $submenu['edit.php'][5][0] = 'Bacon';
    $submenu['edit.php'][10][0] = 'Add Bacon';
    $submenu['edit.php'][16][0] = 'Bacon Tags';
    echo '';
}
function change_post_object_label() {
    global $wp_post_types;
    $labels = &$wp_post_types['post']->labels;
    $labels->name = 'Bacon';
    $labels->singular_name = 'Bacon';
    $labels->add_new = 'Add Bacon';
    $labels->add_new_item = 'Add Bacon';
    $labels->edit_item = 'Edit Bacon';
    $labels->new_item = 'Bacon';
    $labels->view_item = 'View Bacon';
    $labels->search_items = 'Search Bacon';
    $labels->not_found = 'No Bacon found';
    $labels->not_found_in_trash = 'No Bacon found in Trash';
}
add_action( 'init', 'change_post_object_label' );
add_action( 'admin_menu', 'change_post_menu_label' );

source: http://new2wp/snippet/change-wordpress-posts-post-type-news/

WordPress has multiple functions that allow for the type of thing you're trying to do.

Check out:

  • add_post_type_support()
  • remove_post_type_support()
  • register_taxonomy_for_object_type()

If you want to remove a taxonomy, you have to reregister it—using register_taxonomy()—and then omit the post type(s) you don't want from the $object_type argument's array. Then, make sure you're hooking to function to a hook that runs after it's first registered (or on the same hook but at a lower priority).

p.s. Sorry I couldn't link to all these (not enough reputation points). All those functions have entries in the codex; just google them.

Since WordPress 4.4 there is a hook that you can use for that use-case, it is called register_taxonomy_args.

It lets you modify the arguments of a taxonomy before it is actually registered and it works even for the internal taxonomies.

Here is an example which sets the 'category' taxonomy urls to hierarchical:

function NAMESPACE_register_taxonomy_args( $args, $taxonomy ) {
  if ( $taxonomy === 'category' ) {
    $args['rewrite']['hierarchical'] = true;
  }
  return $args;
}
add_filter( 'register_taxonomy_args', 'NAMESPACE_register_taxonomy_args', 10, 2 );

本文标签: Addoverwrite a parameter on an existing post typetaxonomy