Server: 03:39 AM
Reset: 11:00 PM
423
31769
76.87
  johnny Admin Pioneer Moderator
Male Avatar
Received: $5.31
Balance: $4.097
Referrals: 9
Posts: 71
Member Since: 04-27-2018
PM User
Achievements Completed
1 Cashout Valued Advertiser

I want to create a custom taxonomy which behaves similarly to the post type as a category behaves to the default posts (on the grounds of /%category%/%postname%/ permalink structure) so that the posts in the custom post types are displayed as www.example.com/custom-post-type/cust om-taxonomy-name/post-nam e Also I want the category meta box to appear only when we add new default post and not when we add a new post in the custom post type and the custom taxonomy box to appear only when we add a new post in the custom post type and not when we add new a default post.

First of all, if you want to show taxonomy meta box only to custom post type then register the taxonomy to only that custom post type by passing the custom post type name as an argument in register_taxonomy. By doing this the taxonomy meta box appears only to custom post type. If your don't want to show category metabox to custom post type then remove the term category as an argument while registering custom post type and instead include the taxonomy slug name like this 'taxonomies' ). here is the code how i achieved that. I have registered a custom taxonomy with slug themes_categories under custom post type themes

function themes_taxonomy() {
register_taxonomy(
'themes_categories', //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
'themes', //post type name
array(
'hierarchical' = true,
'label' , //Display name
'query_var' = true,
'rewrite' = array(
'slug' , // This controls the base slug that will display before each term
'with_front' t display the category base before
)
)
);
}
add_action( 'init', 'themes_taxonomy');

Then to change the permalink i have created following function
function filter_post_type_link($li  nk, $post)
{
if ($post-post_type !)
return $link;

if ($cats ))
$link
return $link;
}
add_filter('post_type_lin k', 'filter_post_type_link', 10, 2);

Then i registered a custom post type with slug themes as below
//Registering Custom Post Type Themes
add_action( 'init', 'register_themepost', 20 );
function register_themepost() {
$labels = array(
'name' ),
'singular_name' ),
'add_new' ),
'add_new_item' ),
'edit_item' ),
'new_item' ),
'view_item' ),
'search_items' ),
'not_found' ),
'not_found_in_trash' ),
'parent_item_colon' ),
'menu_name' ),
);

$args = array(
'labels' = $labels,
'hierarchical' = false,
'description' ,
'supports' ),
'taxonomies' ),
'show_ui' = true,
'show_in_menu' = true,
'menu_position' = 5,
'menu_icon' ,
'show_in_nav_menus' = true,
'publicly_queryable' = true,
'exclude_from_search' = false,
'query_var' = true,
'can_export' = true,
'rewrite' = FALSE),
'public' = true,
'has_archive' ,
'capability_type'
);
register_post_type( 'themes', $args );//max 20 charachter cannot contain capital letters and spaces
}

There are few things you have to remember while registering custom post. change the has_archive parameter to custom post type slug name and another is change the rewrite slug name as 'slug' custom_post_type_slug/%ta xonomy_slug%

Now when you add a new post type in the write post type page... you will see the permalink as http://www.example.com/wordpress/themes/%the mes_categories%/post-name /. If the custom taxonomy for this post is not selected then the permalink will remain http://www.example.com/wordpress

Jan 14th 2019 at 10:11 AM
Options
  johnny Admin Pioneer Moderator
Male Avatar

This simple guide works perfectly for and if there is a better way to do it ... please leave a comment behind
 

Jan 14th 2019 at 10:13 AM
Options
Page 1 of 11