2010-10-03 146 views
2

我有一个“故事”自定义文章类型“艺术家”,作家”分类标准自定义文章类型,分类的永久链接重写的WordPress 3.0.1

我需要在functions.php中的永久链接设置重写规则看起来像这样:

艺术家(分类/分类):
HTTP://www.example的.com /艾萨克-德意志
/%艺术家%

作家(分类/分类):
http://www.example.com/jean-paul-sartre
/%编剧%

故事(自定义文章类型) :
http://www.example.com/issac-deutscher/jean-paul-sartre/the-cat-is-under-the-table
/%artist%/%writer%/%story%

我已经尝试了一些我在博客中找不到的代码,但是无法弄清楚如何解决这个问题。

我在的WordPress 3.0.1

回答

2

这确实为故事和艺术家的伎俩工作,而不是编剧:

add_action('init', 'custom_init'); 
add_filter('post_type_link', 'story_permalink', 10, 3); 

function custom_init(){ 
    $story = array( 
    'query_var' => true, 
    'rewrite' => false, 
    ); 
    $artist = array(
    'query_var' => true, 
    'rewrite' => true 
    ); 
    $writer = array(
     'query_var' => true, 
     'rewrite' => true 
    ); 

    register_post_type('story', $story); 
    register_taxonomy('artist', 'story', $artist); 
    register_taxonomy('writer', 'story', $writer); 

    global $wp_rewrite; 
    $story_structure = '/%artist%/%writer%/%story%'; 
    $wp_rewrite->add_rewrite_tag("%story%", '([^/]+)', "story="); 
    $wp_rewrite->add_permastruct('story', $story_structure, false); 
} 

function story_permalink($permalink, $post_id, $leavename){ 
    $post = get_post($post_id); 

    $rewritecode = array(
    '%artist%', 
    '%writer%', 
    $leavename? '' : '%postname%', 
    $leavename? '' : '%pagename%', 
    ); 

    if('' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft'))){ 

     if (strpos($permalink, '%artist%') !== FALSE){ 
     $terms = wp_get_object_terms($post->ID, 'artist'); 
     if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $artist = $terms[0]->slug; 
     else $artist = 'unassigned-artist';   
     } 

    if (strpos($permalink, '%writer%') !== FALSE){ 
     $terms = wp_get_object_terms($post->ID, 'writer'); 
     if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $writer = $terms[0]->slug; 
     else $writer = 'unassigned-writer';   
    }   

    $rewritereplace = array(
     $artist, 
     $writer, 
     $post->post_name, 
     $post->post_name, 
    ); 
    $permalink = str_replace($rewritecode, $rewritereplace, $permalink); 
    } 
    else{ 
    } 
    return $permalink; 
} 

希望它能帮助。

+0

嗨@maukoquiroga!我复制了你的代码并修改了一下,我已经替换了cpt和taxonomy的名字,并彻底删除了作者的分类。不能让它工作,只有404的。认为你可以帮助我吗?请看看: http://pastebin.com/Q19amsZL – 2013-01-10 10:46:36

相关问题