自定义POST永久链接中的父和子分类插件

时间:2016-01-03 作者:Mihir

我已经注册了一个自定义帖子类型“book”和一个分类法“authors”,如下所示:

add_action( \'init\', \'post_book_init\' );

function post_book_init() {

    $labels = array(
        \'name\'               => \'BOOKs\',
        \'singular_name\'      => \'BOOK\',
        \'menu_name\'          => \'BOOKs\',
        \'name_admin_bar\'     => \'BOOK\',
        \'add_new\'            => \'Add New\',
        \'add_new_item\'       => \'Add New BOOK\',
        \'new_item\'           => \'New BOOK\',
        \'edit_item\'          => \'Edit BOOK\',
        \'view_item\'          => \'View BOOK\',
        \'all_items\'          => \'All BOOKs\',
        \'search_items\'       => \'Search BOOKs\',
        \'parent_item_colon\'  => \'Parent BOOKs\',
        \'not_found\'          => \'No BOOK found.\',
        \'not_found_in_trash\' => \'No BOOK found in Trash.\'
    );

    $args = array(
        \'labels\'             => $labels,
        \'taxonomies\'         => array(\'authors\'),       
        \'public\'             => true,
        \'publicly_queryable\' => true,
        \'show_ui\'            => true,
        \'show_in_menu\'       => true,
        \'query_var\'          => true,
        \'rewrite\'            => false,  
        \'with_front\' => false,      
        \'capability_type\'    => \'post\',
        \'has_archive\'        => \'bookarchives\',
        \'hierarchical\'       => false,
        \'menu_position\'      => null,
        \'supports\'           => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'trackbacks\',\'custom-fields\',\'comments\',\'revisions\',\'page-attributes\',\'post-formats\' )
    );
    register_post_type(\'book\', $args );

$labels = array(
        \'name\'              => \'Authors\',
        \'singular_name\'     => \'Author\',
        \'search_items\'      => \'Search Author\',
        \'all_items\'         => \'All Authors\',
        \'parent_item\'       => \'Parent Author\',
        \'parent_item_colon\' => \'Parent Author:\',
        \'edit_item\'         => \'Edit Author\',
        \'update_item\'       => \'Update Author\',
        \'add_new_item\'      => \'Add New Author\',
        \'new_item_name\'     => \'New Author Name\',
        \'menu_name\'         => \'Author\'
    );

    $args = array(
        \'hierarchical\'      => true,
        \'labels\'            => $labels,
        \'show_ui\'           => true,
        \'show_admin_column\' => true,
        \'show_in_nav_menus\' => true,
        \'query_var\'         => true,
        \'rewrite\'           => array( \'slug\' => \'authors\' ),
    );

    register_taxonomy(\'authors\', \'book\', $args );

        global $wp_rewrite;
    $book_structure = \'/book/%authors%/%book%-blah-blah-blah\';
$wp_rewrite->add_rewrite_tag("%book%", \'([^/]+)\', "book=");
$wp_rewrite->add_permastruct(\'book\', $book_structure, false);

    unset($labels);
    unset($args);
}
我想在permalink中使用分类术语,并在注册post&;后应用重写规则;分类学并使用以下代码替换了分类术语:

add_filter(\'post_type_link\', \'author_permalink_structure\', 10, 4);
function author_permalink_structure($post_link, $post, $leavename, $sample) {
    if (false !== strpos($post_link, \'%authors%\')) {
        $author_type_term = get_the_terms($post->ID, \'authors\');
        if (!empty($author_type_term))
        {
            $author_slug = array_pop($author_type_term)->slug;
            $parent_slug=get_term(get_term_by(\'slug\',$author_slug,\'authors\')->parent,\'authors\')->slug;
            $post_link = str_replace(\'%authors%\', $author_slug, $post_link);
        }
        else
            $post_link = str_replace(\'%authors%\', \'uncategorized\', $post_link);
    }
    return $post_link;
}
因此,以下链接可以很好地工作:localhost/book/[authors]/[book title]-blah blah blahandlocalhost/book/[authors],但我还需要具有子分类法的父分类法slug,如[父分类法slug]/[子分类法slug]我尝试将父分类法与;子元素使用“/”替换,但返回404页未找到,尽管使用“-”合并效果很好。

$author_slug = array_pop($author_type_term)->slug;
$parent_slug=get_term(get_term_by(\'slug\',$author_slug,\'authors\')->parent,\'authors\')->slug;
$post_link = str_replace(\'%authors%\', $parent_slug . "-" .$author_slug , $post_link);

How can I use "[parent-taxonomy-slug]/[child-taxonomy-slug]" in permalink?

1 个回复
最合适的回答,由SO网友:Milo 整理而成

要处理额外的URL段,您需要add another rewrite rule 类似于生成的add_permastruct, 但有一个额外的捕获组和斜杠来增加术语级别:

add_rewrite_rule(
    \'book/([^/]+)/([^/]+)/([^/]+)-blah-blah-blah?(:/([0-9]+))?/?$\',
    \'index.php?post_type=book&name=$matches[3]\',
    \'top\'
);
此外,正如我在评论中提到的,查询var和slugauthor 已经由core使用,您可能应该更改这些。

在您的post_type_link 函数,您应该在尝试访问术语/父术语的属性之前检查它们是否存在,这将产生警告debugging enabled.

相关推荐

Force pretty permalinks?

我正在构建一个插件,该插件将用于单个站点,并依赖于add_rewrite_rule 要工作,需要打开永久链接。打开它们并不困难,因为它只是一个站点,但我担心其中一个管理员可能会在不知道自己在做什么的情况下关闭它,并破坏该站点。如何以编程方式强制保持漂亮的永久链接?