Skip to content

Instantly share code, notes, and snippets.

@mrbobbybryant
Last active February 10, 2018 13:00
Show Gist options
  • Save mrbobbybryant/fdb76c7129370062956f to your computer and use it in GitHub Desktop.
Save mrbobbybryant/fdb76c7129370062956f to your computer and use it in GitHub Desktop.
How to Create a Custom WordPress Taxonomy
<?php
function dwwp_register_taxonomy() {
$singular = 'Location';
$plural = 'Locations';
$slug = str_replace( ' ', '_', strtolower( $singular ) );
$labels = array(
'name' => $plural,
'singular_name' => $singular,
'search_items' => 'Search ' . $plural,
'popular_items' => 'Popular ' . $plural,
'all_items' => 'All ' . $plural,
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => 'Edit ' . $singular,
'update_item' => 'Update ' . $singular,
'add_new_item' => 'Add New ' . $singular,
'new_item_name' => 'New ' . $singular . ' Name',
'separate_items_with_commas' => 'Separate ' . $plural . ' with commas',
'add_or_remove_items' => 'Add or remove ' . $plural,
'choose_from_most_used' => 'Choose from the most used ' . $plural,
'not_found' => 'No ' . $plural . ' found.',
'menu_name' => $plural,
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => $$slug ),
);
register_taxonomy( $$slug, 'my_post_type', $args );
}
add_action( 'init', 'dwwp_register_taxonomy' );
@Blueacedesign
Copy link

Thanks for fixing stnc, hour later pop on head.

@renzosalvador
Copy link

thanks guys,

@Saad-ezz-wp
Copy link

thanks stnc for fixing

@GhassenAskri
Copy link

function dwwp_register_taxonomy() {
$singular = 'Location';
$plural = 'Locations';
$slug = str_replace( ' ', '_', strtolower( $singular ) );
$labels = array(
'name' => $plural,
'singular_name' => $singular,
'search_items' => 'Search ' . $plural,
'popular_items' => 'Popular ' . $plural,
'all_items' => 'All ' . $plural,
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => 'Edit ' . $singular,
'update_item' => 'Update ' . $singular,
'add_new_item' => 'Add New ' . $singular,
'new_item_name' => 'New ' . $singular . ' Name',
'separate_items_with_commas' => 'Separate ' . $plural . ' with commas',
'add_or_remove_items' => 'Add or remove ' . $plural,
'choose_from_most_used' => 'Choose from the most used ' . $plural,
'not_found' => 'No ' . $plural . ' found.',
'menu_name' => $plural
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => $slug )
);
register_taxonomy( $slug, 'Ouveure', $args );
}
add_action( 'init', 'dwwp_register_taxonomy' );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment