<?php | |
/* | |
Term Archive Pages: | |
- http://example.com/recipes/dinner/ | |
- http://example.com/recipes/breakfast,brunch/ | |
Single Recipe Pages: | |
- http://example.com/recipes/dinner/soup-title/ | |
- http://example.com/recipes/breakfast,brunch/egg-dish-title/ | |
*/ | |
add_action( 'init', 'register_my_types' ); | |
function register_my_types() { | |
register_post_type( 'recipes', | |
array( | |
'labels' => array( | |
'name' => __( 'Recipes' ), | |
'singular_name' => __( 'Recipee' ) | |
), | |
'public' => true, | |
'has_archive' => true, | |
) | |
); | |
register_taxonomy( 'occasion', array( 'recipes' ), array( | |
'hierarchical' => true, | |
'label' => 'Occasions' | |
) | |
); | |
} | |
// Add our custom permastructures for custom taxonomy and post | |
add_action( 'wp_loaded', 'add_clinic_permastructure' ); | |
function add_clinic_permastructure() { | |
global $wp_rewrite; | |
add_permastruct( 'occasion', 'recipes/%occasion%', false ); | |
add_permastruct( 'recipes', 'recipes/%occasion%/%recipes%', false ); | |
} | |
// Make sure that all links on the site, include the related texonomy terms | |
add_filter( 'post_type_link', 'recipe_permalinks', 10, 2 ); | |
function recipe_permalinks( $permalink, $post ) { | |
if ( $post->post_type !== 'recipes' ) | |
return $permalink; | |
$terms = get_the_terms( $post->ID, 'occasion' ); | |
if ( ! $terms ) | |
return str_replace( '%occasion%/', '', $permalink ); | |
$post_terms = array(); | |
foreach ( $terms as $term ) | |
$post_terms[] = $term->slug; | |
return str_replace( '%occasion%', implode( ',', $post_terms ) , $permalink ); | |
} | |
// Make sure that all term links include their parents in the permalinks | |
add_filter( 'term_link', 'add_term_parents_to_permalinks', 10, 2 ); | |
function add_term_parents_to_permalinks( $permalink, $term ) { | |
$term_parents = get_term_parents( $term ); | |
foreach ( $term_parents as $term_parent ) | |
$permlink = str_replace( $term->slug, $term_parent->slug . ',' . $term->slug, $permalink ); | |
return $permlink; | |
} | |
// Helper function to get all parents of a term | |
function get_term_parents( $term, &$parents = array() ) { | |
$parent = get_term( $term->parent, $term->taxonomy ); | |
if ( is_wp_error( $parent ) ) | |
return $parents; | |
$parents[] = $parent; | |
if ( $parent->parent ) | |
get_term_parents( $parent, $parents ); | |
return $parents; | |
} |
This comment has been minimized.
This comment has been minimized.
hi, Here i Wrote a article with your example . http://kvcodes.com/2014/03/custom-post-type-and-taxonomy-permalink-creation/ |
This comment has been minimized.
This comment has been minimized.
is it possible to get the taxonomy archive pagination to work.. gives me an 404 error when going to --> example: /recipes/dinner/page/2/ |
This comment has been minimized.
This comment has been minimized.
I want the permalink structure like this one below |
This comment has been minimized.
This comment has been minimized.
Spendid @adamsmark.ghaji@gmail.com.www.instagram.com/evang.adams mark |
This comment has been minimized.
This comment has been minimized.
Brilliant , big thanks man |
This comment has been minimized.
This comment has been minimized.
This is great! Exactly what I needed! I'm trying to get the posts to order by title on the Taxonomy page and not having much luck, is there an easy way of doing this I've missed? Thanks |
This comment has been minimized.
This comment has been minimized.
Hi, If anyone still has a problem with the link structure in WordPress, then I would like to present you a plugin that solves this problem: https://wordpress.org/plugins/wp-better-permalinks/ This plugin allows you do what with the code above and more - and all this with a few clicks in the admin panel. By using it you can achieve a friendly link structure: Custom Post Type> Single Term> Post I invite you to check my plugin and if you like it - to recommend to others. I will be very grateful! |
This comment has been minimized.
This comment has been minimized.
This is exactly what I have been looking for, it works great for me but I do get the following error message 'Undefined variable: permlink in /Users/...' 'on line 89'. |
This comment has been minimized.
This comment has been minimized.
after adding $wp_rewrite->flush_rules(); in wp_load action 404 page is appeare how to solve that . |
This comment has been minimized.
This comment has been minimized.
Great Work, i solved a little problem adding a line here: add_filter( 'term_link', 'add_term_parents_to_permalinks', 10, 2 ); because without this i retrieve an error when i navigate custom taxonomy archive. And i have a question: |
This comment has been minimized.
This comment has been minimized.
Note: taxonomy has to be registered before post type. |
This comment has been minimized.
This comment has been minimized.
Great Works |
This comment has been minimized.
This comment has been minimized.
how to replace , by / |
This comment has been minimized.
This comment has been minimized.
Hi! Thank for the example! But can somebody help me to do for this example own structure? But only for the three taxonomies: www.mysite/type-deal/type-realty/city/address. In my situation address is post name for wordpress. |
This comment has been minimized.
This comment has been minimized.
i have a post type with 2 different categories |
This comment has been minimized.
This comment has been minimized.
I wouldn't use category and tags with CPT's. Better to register custom taxonomy terms and separate standard posts from custom post types. |
This comment has been minimized.
This comment has been minimized.
Thank you for the example. At least it got me on the right track and I didn't miss anything. |
This comment has been minimized.
This comment has been minimized.
Hi. If I want to add a non-hierarchical taxonomy like WordPress tags to your code.
As a result, the link structure will be as follows:
How else to add “recipes” to this taxonomy so that:
Tried like this:
In this case, the structure How to tie these 2 taxonomies together? Thank. |
This comment has been minimized.
This comment has been minimized.
Looks like there is a typo in the code... Just change the Please let me know your results |
This comment has been minimized.
Gracias!. Esto me ha sido muy util!
Thank you!! This has been very useful to me!