TeddyCoder | Web Freelancer in Hochiminh City

Add slug to permalink

add_filter('post_type_link', 'product_type_permalink', 10, 4);
function product_type_permalink($post_link, $post, $leavename, $sample) {
//If is custom post type "product"
if ($post->post_type == 'blog') {
// Get current post object
global $post;
// Get current value from custom taxonomy "product-type"
$terms = get_the_terms($post->id, 'blogcat');
// Define category from "slug" of taxonomy object
$term = $terms[0]->slug;
// Re-structure permalink with string replace to include taxonomy value and post name
$permalink = str_replace('blog/', 'blog/' . $term . '/', $post_link);

if(!$term) {
$permalink = str_replace('blog/', 'blog/blog_cate/', $post_link);
}

}
return $permalink;
}

BACK