小编mau*_*oga的帖子

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

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

通过执行以下操作,我设法获得了/%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%', …
Run Code Online (Sandbox Code Playgroud)

wordpress rewrite permalinks taxonomy custom-post-type

5
推荐指数
0
解决办法
2801
查看次数