On Crunchify, we have published number of articles on WordPress Custom Post Type. One of the very popular tutorial is about How to Create WordPress Custom Post Type (CPT) and Taxonomy.
In this tutorial we will go over steps on how to add custom taxonomy in custom post type permalink?
Step-1. What we are trying?
If you have a custom taxonomy called tourist
and a custom post type called attraction
.
For attractions, what if I want to have the following permalink structure: crunchify.com/<tourist_name>/<attraction_name>
Well, you are at right place.
Step-2.
Please use below Custom Post Type code in functions.php
// ================================= Create Attraction Custom Post Type ================================= function crunchify_create_the_attaction_posttype() { $labels = array( 'name' => _x( 'Attraction', 'Post Type General Name', 'CRUNCHIFY_TEXT_DOMAIN' ), 'singular_name' => _x( 'Attraction', 'Post Type Singular Name', 'CRUNCHIFY_TEXT_DOMAIN' ), 'menu_name' => esc_html__( 'Attraction', 'CRUNCHIFY_TEXT_DOMAIN' ), 'parent_item_colon' => esc_html__( 'Parent Attraction', 'CRUNCHIFY_TEXT_DOMAIN' ), 'all_items' => esc_html__( 'All Attraction', 'CRUNCHIFY_TEXT_DOMAIN' ), 'view_item' => esc_html__( 'View Attraction', 'CRUNCHIFY_TEXT_DOMAIN' ), 'add_new_item' => esc_html__( 'Add New Attraction', 'CRUNCHIFY_TEXT_DOMAIN' ), 'add_new' => esc_html__( 'Add New', 'CRUNCHIFY_TEXT_DOMAIN' ), 'edit_item' => esc_html__( 'Edit Attraction', 'CRUNCHIFY_TEXT_DOMAIN' ), 'update_item' => esc_html__( 'Update Attraction', 'CRUNCHIFY_TEXT_DOMAIN' ), 'search_items' => esc_html__( 'Search Attraction', 'CRUNCHIFY_TEXT_DOMAIN' ), 'not_found' => esc_html__( 'Not Found', 'CRUNCHIFY_TEXT_DOMAIN' ), 'not_found_in_trash' => esc_html__( 'Not found in Trash', 'CRUNCHIFY_TEXT_DOMAIN' ), ); $args = array( 'label' => esc_html__( 'attraction', 'CRUNCHIFY_TEXT_DOMAIN' ), 'description' => esc_html__( 'Attraction', 'CRUNCHIFY_TEXT_DOMAIN' ), 'labels' => $labels, 'supports' => array( 'title','editor','thumbnail'), 'taxonomies' => array( 'genres' ), 'hierarchical' => false, 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'show_in_nav_menus' => true, 'show_in_admin_bar' => true, 'menu_position' => 100, 'can_export' => true, 'has_archive' => __( 'attraction' ), 'exclude_from_search' => false, 'publicly_queryable' => true, 'query_var' => true, 'show_admin_column' => true, 'capability_type' => 'post', 'rewrite' => array('slug' => 'attraction/%tourist%'), ); register_post_type( 'attraction', $args ); } add_action( 'init', 'crunchify_create_the_attaction_posttype', 0 );
Step-3.
Please use below Custom Taxonomy code in functions.php
// ================================= Custom Post Type Taxonomies ================================= function crunchify_create_the_attaction_taxonomy() { register_taxonomy( 'tourist', // This is a name of the taxonomy. Make sure it's not a capital letter and no space in between 'attraction', //post type name array( 'hierarchical' => true, 'label' => 'Attractions', //Display name 'query_var' => true, 'has_archive' => true, 'rewrite' => array('slug' => 'attraction') ) ); } add_action( 'init', 'crunchify_create_the_attaction_taxonomy');
Step-4.
For Attraction Post Type Single Link, add Category Slug.
function crunchify_create_post_link( $post_link, $id = 0 ){ $post = get_post($id); if ( is_object( $post ) ){ $terms = wp_get_object_terms( $post->ID, 'tourist' ); if( $terms ){ return str_replace( '%tourist%' , $terms[0]->slug , $post_link ); } } return $post_link; } add_filter( 'post_type_link', 'crunchify_create_post_link', 1, 3 );
You will start seeing another WordPress left panel menu after that.
Let me know if you face any issue with this code. Happy blogging.
Check out another detailed article on Custom Post Type.