I need a permalink to be in the form /writer/%term_id%/%term_slug/, as /writer/123/hand-made-shoes
I get the following code but it do not resolve the correct permalink. What could I be missing?
function writer_structure(){
global $wp_rewrite;
$writer_structure = '/writers/%writer_id%/%writer%';
$wp_rewrite->add_rewrite_tag('%writer%', '([^/]+)', "writer=");
$wp_rewrite->add_permastruct('writer', $writer_structure, false);
}
function writer_permalink($permalink, $post_id, $leavename){
if (get_option('permalink_structure') != ''){
$post = get_post($post_id);
$rewritecode = array(
'%writer_id%',
'%writer%'
);
if (strpos($permalink, '%writer%') !== FALSE){
$terms = wp_get_object_terms($post->ID, 'writer');
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $writer = $terms[0]->slug;
else $writer = '';
}
$rewritereplace = array(
$post->ID,
$writer
);
$permalink = str_replace($rewritecode, $rewritereplace, $permalink);
}
return $permalink;
}
Thanks.