Quickly add WordPress custom taxonomies with custom functions

Quickly Add WordPress Custom Post Types via Custom FunctionsIn this article, I introduced if you quickly add a WordPress custom post type through a custom function, the brain turned a little faster friends may have thought of what I'm going to say in this article, quickly add a WordPress custom taxonomy through a custom function, as opposed to adding a custom post type, we are through the official register_ taxonomy function to add a custom taxonomy, the following is the full code of the function:

  function create_taxs($tax_slug, $hook_type, $tax_name) {
    // Customize taxonomy labels
    $labels_tax = array(
      'name' => $tax_name,
      'singular_name' => $tax_name,
      'search_items' => 'search' . $tax_name, 'search_items' => 'search' .
      'all_items' => 'all' . $tax_name, 'parent_item' => 'all' .
      'parent_item' => 'parent' . $tax_name, 'parent_item_collection' => 'all' .
      'parent_item_colon' => 'parent' . $tax_name, 'parent_item_colon' => 'parent' .
      'edit_item' => 'edit' . $tax_name, 'edit_item' => 'edit' .
      'update_item' => 'Update' . $tax_name, 'add_new_item' => 'update' .
      'add_new_item' => 'Add New' . $tax_name, 'new_item_name' => 'add new' .
      'new_item_name' => 'New' . $tax_name . 'name', 'new_item_name' => 'name', 'new_item_name' => 'new' .
      'menu_name' => $tax_name, .
    ).

    // Customize the taxonomy parameters
    $args_tax = array(
      'hierarchical' => true,
      'labels' => $labels_tax,
      'show_ui' => true,
      'show_admin_column' => true,
      'query_var' => true, 'query_var' => true, 'rewrite' => array
      'rewrite' => array( 'slug' => $tax_slug ),
    ).

    register_taxonomy( $tax_slug, array( $hook_type ), $args_tax );
  }

It is also very easy to use:

create_taxs("date", 'post', "date");

Where date is the slug of the custom taxonomy, post is the type of post to be associated to (here it is the default post, it is ok to use other custom post types), and date is the name of the custom taxonomy displayed in the backend menu.

For the average WordPress user, this might be the trick, while for the WordPress veteran or PHP expert, this is a carve-out at best, right? Feel free to take a shot at it!

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *