How create custom post type URL with category slug


Hello Friends!!

I am here going to share my knowledge  about one  more important hooks of the wordpress. If you have created a custom post type and want to add their custom taxonomy category slug as prefix in post URL without any changes in wordpress core files then follow given steps. You will need to add given below code into your theme function.php file.

  1. Register new custom post type if you have already not done it
    if(!function_exists('mrweb_acticle_post_type_func')){
     function mrweb_acticle_post_type_func()
     {
    
     $labels = array(
     'name' => __( 'Article', 'mrwebsolution' ),
     'singular_name' => __( 'Article', 'mrwebsolution' ),
     'add_new' => __( 'Add New', 'mrwebsolution' ),
     'add_new_item' => __( 'Add New Article', 'mrwebsolution' ),
     'edit_item' => __( 'Edit Article', 'mrwebsolution' ),
     'new_item' => __( 'New Article', 'mrwebsolution' ),
     'view_item' => __( 'View Article', 'mrwebsolution' ),
     'search_items' => __( 'Search Article', 'mrwebsolution' ),
     'not_found' => __( 'No Article Found', 'mrwebsolution' ),
     'not_found_in_trash' => __( 'No article Found In Trash', 'mrwebsolution' ),
     'parent_item_colon' => '',
     'menu_name' => __( 'Article', 'mrwebsolution' )
     );
     $args = array(
     'labels' => $labels,
     'menu_icon' => 'dashicons-admin-post',
     'public' => true,
     'publicly_queryable' => true,
     'show_ui' => true,
     'show_in_menu' => true,
     'query_var' => true,
     'rewrite' => array( 'slug' => 'articles/%mrweb_article_term%' ),
     'capability_type' => 'post',
     'hierarchical' => true,
     'menu_position' => null,
     'has_archive' => 'articles',
     'supports' => array( 'title', 'editor','thumbnail', 'page-attributes', 'comments', 'author')
     );
    
     register_post_type( 'mrweb_article', $args);
     }
     }
     add_action('init', 'mrweb_acticle_post_type_func');
  2.  Register new custom taxonomy type if you have already not done it
    // hook into the init action and call create_book_taxonomies when it fires
     add_action( 'init', 'create_mrweb_article_taxonomies', 0 );
    
     // create two taxonomies, genres and writers for the post type "book"
     if(!function_exists('create_mrweb_article_taxonomies'))
     {
     function create_mrweb_article_taxonomies() {
     $labels = array(
     'name' => __('Blog Categories', 'mrwebsolution'),
     'singular_name' => __('Category', 'mrwebsolution'),
     'search_items' => __('Search Categories', 'mrwebsolution'),
     'all_items' => __('All Categories', 'mrwebsolution'),
     'parent_item' => __('Parent', 'mrwebsolution'),
     'parent_item_colon' => __('Parent:', 'mrwebsolution'),
     'edit_item' => __('Edit Category', 'mrwebsolution'),
     'update_item' => __('Update Category', 'mrwebsolution'),
     'add_new_item' => __('Add New Category', 'mrwebsolution'),
     'new_item_name' => __('New Category', 'mrwebsolution'),
     'menu_name' => __('Categories', 'mrwebsolution'),
     );
     $args = array(
     'hierarchical' => true,
     'labels' => $labels,
     'show_ui' => true,
     'show_admin_column' => true,
     'query_var' => true,
     'rewrite' => array( 'slug' => 'articles','with_front' => false ),
     );
     register_taxonomy( 'mrweb_article_term', array( 'mrweb_article' ), $args ); 
     }
     }
  3. Add a new hooks filter post_type_link
    /** filter URL link for post type url **/
     add_filter('post_type_link', 'acrticles_permalink_structure', 10, 4);
     function acrticles_permalink_structure($post_link, $post, $leavename, $sample)
     {
     if ( false !== strpos( $post_link, '%mrweb_article_term%' ) ) {
     $event_type_term = get_the_terms( $post->ID, 'mrweb_article_term' );
     if($event_type_term)
     $post_link = str_replace( '%mrweb_article_term%', array_pop( $event_type_term )->slug, $post_link );
     }
     return $post_link;
     }
  4. That’s it 🙂

Share with your friend 🙂

Advertisements

Sr. Software Engineer

Tagged with: ,
Posted in Wordpress

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: