Wordpress中的多个自定义永久链接结构

mau*_*oga 5 wordpress rewrite permalinks taxonomy custom-post-type

我有一个“自定义帖子类型”“故事”和两个分类法,“艺术家”“作家”

通过执行以下操作,我设法获得了/%artist /%writer%/%story%这样的自定义永久链接结构(恢复的代码):

    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;
    }
Run Code Online (Sandbox Code Playgroud)

这给了我类似/ jason / john / the-cat-is-under-the-table的功能

但是我真正需要的结构是:

/ artists /%artist%
/ writers /%writer%
/ stories /%story%

也许太简单了,但是却无法真正弄清楚如何完成此操作,因为我不知道如何在同质的自定义永久链接结构中处理自定义帖子类型和分类法。

任何解决此问题的帮助将不胜感激。