开发者

Change post permalink structure on WordPress to use custom taxonomy

I want to change the post permalink schema on my WordPress 3.0-beta1 to use my new custom taxonomy.

Today I can use /%category%/%postname%/ and the /my-category/my-post/ URL, that's nice but I need to use another taxonomy instead "category" one.

I tried to use 开发者_如何学Python/%acervo%/%postname%/ but my URLs came with %acervo% on the URL instead the name of the "Acevo" (my taxonomy name) wich the post belongs to.

I found something related to WP_Rewrite but without sucess...


You could try using the WordPress plugin, No Category Base, and then hard code the taxonomy in with the postname wildcard, like so:

/acervo/%postname%/

Note that acervo does not have the percentage signs since it's "hard coded" and not a wildcard.


Simply change your category base in Dashboard/Settings/Permalinks No need to get rid of the category base and then add it in again.


I get it... Changed the permalink structure to /%acervos%/%postname%/ and then dived into WP_Rewrite and added a new "replacement tag" replacing %acervos% with (.*) regexp.


This should to the trick.

function acervo_permalink($permalink, $post_id, $leavename){
    if (get_option('permalink_structure') != ''){
        $post = get_post($post_id);
        $rewritecode = array(
            '%acervo%'
        );
        if (strpos($permalink, '%acervo%') !== FALSE){   
            $terms = wp_get_object_terms($post->ID, 'acervo');  
            if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $acervo = $terms[0]->slug;
            else $acervo = '';
        }
        $rewritereplace = array(
            $acervo
        );
        $permalink = str_replace($rewritecode, $rewritereplace, $permalink);
    } 
    return $permalink;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜