【问题标题】:Custom Post Type and Taxonomy Permalink Rewrite in WordPress 3.0.1WordPress 3.0.1 中的自定义帖子类型和分类固定链接重写
【发布时间】:2010-12-20 08:45:00
【问题描述】:

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

我需要在functions.php 中设置rewrite rules 以使permalinks 看起来像这样:

艺术家(分类/类别):
http://www.example.com/isaac-deutscher
(/%艺术家%)

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

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

我尝试了一些在博客中找到的代码,但没有成功,不知道如何解决。

我正在使用 Wordpress 3.0.1

【问题讨论】:

    标签: wordpress rewrite taxonomy permalinks custom-post-type


    【解决方案1】:

    这对 Story 和 Artist 有用,但对 Writer 无效:

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

    希望对你有帮助。

    【讨论】:

    • 嗨@maukoquiroga!我已经复制了您的代码并对其进行了一些修改,我已经替换了 cpt 和分类名称并完全删除了作者分类。虽然不能让它工作,但只能得到404。你觉得你能帮我吗?请看:pastebin.com/Q19amsZL
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-01
    • 2018-07-03
    • 1970-01-01
    • 2021-03-05
    • 2013-08-03
    • 2012-05-05
    • 1970-01-01
    相关资源
    最近更新 更多