2015-11-01 144 views
0

我有一个“画廊”为custom_post_type和“唱片”为taxonomry_name固定链接结构为我custom_post_type

我怎样才能达到这个结构: mydomain.com/gallery/albums/{taxonomy_term}/{post}

我已经试过类似下面的例子,但它没有工作,也许我还没有使用它正确

add_filter('rewrite_rules_array', 'mmp_rewrite_rules'); 
function mmp_rewrite_rules($rules) { 
    $newRules = array(); 
    $newRules['basename/(.+)/(.+)/(.+)/?$'] = 'index.php?gallery=$matches[3]'; // my custom structure will always have the post name as the 4th uri segment 
    $newRules['basename/(.+)/?$']    = 'index.php?albums=$matches[1]'; 

    return array_merge($newRules, $rules); 
} 

function filter_post_type_link($link, $post) 
{ 
    if ($post->post_type != 'gallery') 
     return $link; 

    if ($cats = get_the_terms($post->ID, 'albums')) 
    { 
     $link = str_replace('%albums%', get_taxonomy_parents(array_pop($cats)->term_id, 'albums', false, '/', true), $link); // see custom function defined below 
    } 
    return $link; 
} 
add_filter('post_type_link', 'filter_post_type_link', 10, 2); 

function get_taxonomy_parents($id, $taxonomy, $link = false, $separator = '/', $nicename = false, $visited = array()) {  
    $chain = ''; 
    $parent = &get_term($id, $taxonomy); 

    if (is_wp_error($parent)) { 
     return $parent; 
    } 

    if ($nicename)  
     $name = $parent -> slug;   
else  
     $name = $parent -> name; 

    if ($parent -> parent && ($parent -> parent != $parent -> term_id) && !in_array($parent -> parent, $visited)) {  
     $visited[] = $parent -> parent;  
     $chain .= get_taxonomy_parents($parent -> parent, $taxonomy, $link, $separator, $nicename, $visited); 

    } 

    if ($link) { 
     // nothing, can't get this working :(
    } else  
     $chain .= $name . $separator;  
    return $chain;  
} 

回答

0

我用下面的代码解决了我的问题:

// Add our custom permastructures for custom taxonomy and post 
add_action('wp_loaded', 'add_album_permastructure'); 
function add_album_permastructure() { 
    global $wp_rewrite; 
    add_permastruct('albums', 'gallery/%albums%', false); 
    add_permastruct('gallery', 'gallery/%albums%/%gallery%', false); 
} 
// Make sure that all links on the site, include the related texonomy terms 
add_filter('post_type_link', 'gallery_permalinks', 10, 2); 
function gallery_permalinks($permalink, $post) { 
    if ($post->post_type !== 'gallery') 
     return $permalink; 
    $terms = get_the_terms($post->ID, 'albums'); 

    if (! $terms) 
     return str_replace('%albums%/', '', $permalink); 
    $post_terms = array(); 
    foreach ($terms as $term) 
     $post_terms[] = $term->slug; 
    return str_replace('%albums%', 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; 
} 

将此添加到fucntions.php,然后刷新我的永久链接结构从设置 - >永久