Wordpress-Add custom taxonomies tags to permalinks
Hello everyone, today i am going to discuss with you how to add wordpress custom taxonomies to permalinks.
You can refer custom taxonomies or taxonomies from here
Ok first of all, you need to register custom taxonomy with setting "rewrite=true", doing this will enable %custom_taxonomy_tag%
If you want to add custom taxonomies without coding, i prefer this plugin GD CPT tools
After doing all these things, you need to copy paste this function, to your theme's function.php file
Explanation
Line 5 – If the permalink does not contain the %taxo% tag, then we don’t need to translate anything.
Line 12 – Get the taxo terms related to the current post object.
Line 13 – Retrieve the slug value of the first taxo custom taxonomy object linked to the current post.
Line 14 – If no taxo terms are retrieved, then replace our taxo tag with the value no-taxo.
Line 16 – Replace the %taxo% tag with our custom taxonomy slug.
Now in wordpress, dashboard-settings-permalinks, you can write
This way you can add more custom taxonomies, just by adding more functions.
So Simple!!!!!!!
You can refer custom taxonomies or taxonomies from here
Ok first of all, you need to register custom taxonomy with setting "rewrite=true", doing this will enable %custom_taxonomy_tag%
If you want to add custom taxonomies without coding, i prefer this plugin GD CPT tools
After doing all these things, you need to copy paste this function, to your theme's function.php file
add_filter('post_link', 'taxo_permalink', 10, 3);
add_filter('post_type_link', 'taxo_permalink', 10, 3);
function taxo_permalink($permalink, $post_id, $leavename) {
if (strpos($permalink, '%taxo%') === FALSE) return $permalink;
//To Get post
$post = get_post($post_id);
if (!$post) return $permalink;
//To Get taxonomy terms
$terms = wp_get_object_terms($post->ID, 'taxo');
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
else $taxonomy_slug = 'no-taxo';
return str_replace('%taxo%', $taxonomy_slug, $permalink);
}
Explanation
Line 5 – If the permalink does not contain the %taxo% tag, then we don’t need to translate anything.
Line 12 – Get the taxo terms related to the current post object.
Line 13 – Retrieve the slug value of the first taxo custom taxonomy object linked to the current post.
Line 14 – If no taxo terms are retrieved, then replace our taxo tag with the value no-taxo.
Line 16 – Replace the %taxo% tag with our custom taxonomy slug.
Now in wordpress, dashboard-settings-permalinks, you can write
%postname%/%taxo%
This way you can add more custom taxonomies, just by adding more functions.
So Simple!!!!!!!
Subscribe to:
Posts (Atom)
Blog Archive
- February 2011 (5)
- January 2011 (4)
- December 2010 (1)
- October 2010 (5)
About Me
- NiRaJ (asp or php)!!!!!!
- I am basically a software engineer and Website designer & developer (final year student)