WordPress 3.0.1 中的自定义帖子类型和分类永久链接重写

发布于 2024-09-26 02:24:52 字数 545 浏览 4 评论 0原文

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

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

艺术家(分类/类别):
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
/%艺术家%/%作家%/%故事%

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

我正在使用 Wordpress 3.0.1

I have a 'story' Custom Post Type and 'artist', writer' Taxonomies.

I need to set rewrite rules in the functions.php for the permalinks to look like this:

Artist (Taxonomy/Category):
http://www.example.com/isaac-deutscher
(/%artist%)

Writer (Taxonomy/Category):
http://www.example.com/jean-paul-sartre
(/%writer%)

Story (Custom Post Type):
http://www.example.com/issac-deutscher/jean-paul-sartre/the-cat-is-under-the-table
(/%artist%/%writer%/%story%)

I have tried some code I found in blogs without success, and can't figure out how to solve this.

I'm working in Wordpress 3.0.1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪操控生活 2024-10-03 02:24:52

这对故事和艺术家有用,但对作家则不然:

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

希望它有帮助。

This does the trick for Story and Artist, but not for 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;
}

Hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文