I have a custom post type i.e services
& custom taxonomy services_type
I have custom permalink structure as /%category%/%postname%
term1,term2,term3
are different terms under services_type taxonomy & post1,post2,post3,post4
are posts
I i am trying to acheive is the permalinks as
http://example.com/term1/post1
http://example.com/term1/post2
http://example.com/term2/post3
http://example.com/term3/post4
but right now they are as http://example.com/services/post1
Following is what i have used so far
function wpt_services_posttype() {
register_post_type( 'services',
array(
'labels' => array(
'name' => __( 'Services' ),
),
'public' => true,
'supports' => array( 'title','editor','thumbnail','page-attributes','custom-fields'),
'capability_type' => 'post',
'rewrite' => array("slug" => "services/%services_type%"), // Permalinks format
'menu_position' => 6,
'show_ui'=>true,
'query_var'=>true
)
);
}
add_action( 'init', 'wpt_services_posttype' );
//code for adding texonomy
function services_init() {
// create a new taxonomy
register_taxonomy(
'services_type',
'services',
array(
'labels' => array(
'name'=>'services type',
'add_new_item'=>'Add New type ',
'new_item_name'=>"New type" ,
),
'show_ui' => true,
'show_tagcloud' => false,
'hierarchical' => true,
'rewrite' => array( 'slug' => 'services' ),
)
);
}
add_action( 'init', 'services_init' );
add_filter('post_type_link', 'services_permalink_structure', 10, 4);
function services_permalink_structure($post_link, $post, $leavename, $sample)
{
if ( false !== strpos( $post_link, '%services_type%' ) ) {
$service_type_term = get_the_terms( $post->ID, 'service_type' );
$post_link = str_replace( '%services_type%', array_pop( $service_type_term )->slug, $post_link );
}
return $post_link;
}
I have tried various codes but it i am not getting any closer.
Please help