我的custom_post_type的永久链接结构

时间:2015-11-01 09:24:30

标签: wordpress custom-post-type permalinks taxonomy

我有一个“gallery”作为custom_post_type,“albums”作为taxonomry_name

我如何实现这种结构: mydomain.com/gallery/albums/{taxonomy_term} / {交}

我尝试过类似下面的示例,但它没有用,或者我没有正确使用它

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;    
}

1 个答案:

答案 0 :(得分: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 ,然后从设置 - >刷新我的永久链接结构。永久