Skip to content

Instantly share code, notes, and snippets.

@illucent
Created March 22, 2014 01:15
Show Gist options
  • Select an option

  • Save illucent/9699652 to your computer and use it in GitHub Desktop.

Select an option

Save illucent/9699652 to your computer and use it in GitHub Desktop.
function kv_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 );
}
add_filter( 'term_link', 'kv_add_term_parents_to_permalinks', 10, 2 );
function kv_add_term_parents_to_permalinks( $permalink, $term ) {
$term_parents = kv_get_term_parents( $term );
foreach ( $term_parents as $term_parent )
$permlink = str_replace( $term->slug, $term_parent->slug . ',' . $term->slug, $permalink );
return $permlink;
}
function kv_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 )
kv_get_term_parents( $parent, $parents );
return $parents;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment