以自定义分类为类别的自定义邮政类型

时间:2015-03-23 作者:rhand

我已经创建了一个带有自定义分类法公文包类别的自定义帖子类型公文包。我可以创建公文包项目,为其分配公文包类别,并保存这些自定义帖子。但我需要的是permalink来展示domain.com/portfolio-category/portfolio-item-name. 投资组合类别是可变的,具体取决于我选择的类别。

现在我只看到domain.com/portfolio/item-name 名称公文包应该是我附加自定义帖子的自定义分类法的名称,但它不是。似乎只是CPT的一个固定名称。我希望URL像domain.com/design/client-adomain.com/dev/client-b

这是我的代码:

// Register Custom Taxonomy
function portfolio_taxonomy() {

    $labels = array(
        \'name\'                       => _x( \'Portfolio Category\', \'Taxonomy General Name\', \'imagewize_portfolio_plugin\' ),
        \'singular_name\'              => _x( \'Portfolio Category\', \'Taxonomy Singular Name\', \'imagewize_portfolio_plugin\' ),
        \'menu_name\'                  => __( \'Category\', \'imagewize_portfolio_plugin\' ),
        \'all_items\'                  => __( \'All Items\', \'imagewize_portfolio_plugin\' ),
        \'parent_item\'                => __( \'Parent Item\', \'imagewize_portfolio_plugin\' ),
        \'parent_item_colon\'          => __( \'Parent Item:\', \'imagewize_portfolio_plugin\' ),
        \'new_item_name\'              => __( \'New Item Name\', \'imagewize_portfolio_plugin\' ),
        \'add_new_item\'               => __( \'Add New Item\', \'imagewize_portfolio_plugin\' ),
        \'edit_item\'                  => __( \'Edit Item\', \'imagewize_portfolio_plugin\' ),
        \'update_item\'                => __( \'Update Item\', \'imagewize_portfolio_plugin\' ),
        \'separate_items_with_commas\' => __( \'Separate items with commas\', \'imagewize_portfolio_plugin\' ),
        \'search_items\'               => __( \'Search Items\', \'imagewize_portfolio_plugin\' ),
        \'add_or_remove_items\'        => __( \'Add or remove items\', \'imagewize_portfolio_plugin\' ),
        \'choose_from_most_used\'      => __( \'Choose from the most used items\', \'imagewize_portfolio_plugin\' ),
        \'not_found\'                  => __( \'Not Found\', \'imagewize_portfolio_plugin\' ),
    );
    $rewrite = array(
        \'slug\'                       => \'Portfolio Category\',
        \'with_front\'                 => true,
        \'hierarchical\'               => true,
    );
    $args = array(
        \'labels\'                     => $labels,
        \'hierarchical\'               => true,
        \'public\'                     => true,
        \'show_ui\'                    => true,
        \'show_admin_column\'          => true,
        \'show_in_nav_menus\'          => true,
        \'show_tagcloud\'              => true,
        \'rewrite\'                    => $rewrite,
    );
    register_taxonomy( \'portfolio\', array( \'Img_portfolio_cpt\' ), $args );

}

// Hook into the \'init\' action
add_action( \'init\', \'portfolio_taxonomy\', 0 );




if ( ! function_exists(\'img_portfolio_cpt\') ) {

// Register Custom Post Type
function img_portfolio_cpt() {

    $labels = array(
        \'name\'                => _x( \'Portfolio Items\', \'Post Type General Name\', \'imagewize_portfolio_plugin\' ),
        \'singular_name\'       => _x( \'Portfolio\', \'Post Type Singular Name\', \'imagewize_portfolio_plugin\' ),
        \'menu_name\'           => __( \'Portfolio\', \'imagewize_portfolio_plugin\' ),
        \'parent_item_colon\'   => __( \'Parent Item:\', \'imagewize_portfolio_plugin\' ),
        \'all_items\'           => __( \'All Items\', \'imagewize_portfolio_plugin\' ),
        \'view_item\'           => __( \'View Item\', \'imagewize_portfolio_plugin\' ),
        \'add_new_item\'        => __( \'Add New Item\', \'imagewize_portfolio_plugin\' ),
        \'add_new\'             => __( \'Add New\', \'imagewize_portfolio_plugin\' ),
        \'edit_item\'           => __( \'Edit Item\', \'imagewize_portfolio_plugin\' ),
        \'update_item\'         => __( \'Update Item\', \'imagewize_portfolio_plugin\' ),
        \'search_items\'        => __( \'Search Item\', \'imagewize_portfolio_plugin\' ),
        \'not_found\'           => __( \'Not found\', \'imagewize_portfolio_plugin\' ),
        \'not_found_in_trash\'  => __( \'Not found in Trash\', \'imagewize_portfolio_plugin\' ),
    );
    $rewrite = array(
        \'slug\'                => \'portfolio\',
        \'with_front\'          => true,
        \'pages\'               => true,
        \'feeds\'               => true,
    );
    $args = array(
        \'label\'               => __( \'Img_portfolio_cpt\', \'imagewize_portfolio_plugin\' ),
        \'description\'         => __( \'Imagewize Portfolio\', \'imagewize_portfolio_plugin\' ),
        \'labels\'              => $labels,
        \'supports\'            => array( \'title\', \'editor\', \'author\', \'thumbnail\', ),
        \'taxonomies\'          => array( \'portfolio\' ),
        \'hierarchical\'        => true,
        \'public\'              => true,
        \'show_ui\'             => true,
        \'show_in_menu\'        => true,
        \'show_in_nav_menus\'   => true,
        \'show_in_admin_bar\'   => true,
        \'menu_position\'       => 5,
        \'menu_icon\'           => \'dashicons-portfolio\',
        \'can_export\'          => true,
        \'has_archive\'         => true,
        \'exclude_from_search\' => false,
        \'publicly_queryable\'  => true,
        \'rewrite\'             => $rewrite,
        \'capability_type\'     => \'page\',
    );
    register_post_type( \'Img_portfolio_cpt\', $args );

}

// Hook into the \'init\' action
add_action( \'init\', \'img_portfolio_cpt\', 0 );

}

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

我浏览了作为注释添加的两个URL,WordPress Codex文档,如register_taxonomy here 还有一些,并想出了这个代码,似乎确实做到了这一点。主要添加的是添加的过滤器post_type_link 过滤cpt段塞并加载所需术语。

    // Register Custom Taxonomy
    function portfolio_taxonomy() {

        $labels = array(
            \'name\'                       => _x( \'Labels\', \'Taxonomy General Name\', \'imagewize_portfolio_plugin\' ),
            \'singular_name\'              => _x( \'Label\', \'Taxonomy Singular Name\', \'imagewize_portfolio_plugin\' ),
            \'menu_name\'                  => __( \'Labels\', \'imagewize_portfolio_plugin\' ),
            \'all_items\'                  => __( \'All Items\', \'imagewize_portfolio_plugin\' ),
            \'parent_item\'                => __( \'Parent Item\', \'imagewize_portfolio_plugin\' ),
            \'parent_item_colon\'          => __( \'Parent Item:\', \'imagewize_portfolio_plugin\' ),
            \'new_item_name\'              => __( \'New Item Name\', \'imagewize_portfolio_plugin\' ),
            \'add_new_item\'               => __( \'Add New Item\', \'imagewize_portfolio_plugin\' ),
            \'edit_item\'                  => __( \'Edit Item\', \'imagewize_portfolio_plugin\' ),
            \'update_item\'                => __( \'Update Item\', \'imagewize_portfolio_plugin\' ),
            \'separate_items_with_commas\' => __( \'Separate items with commas\', \'imagewize_portfolio_plugin\' ),
            \'search_items\'               => __( \'Search Items\', \'imagewize_portfolio_plugin\' ),
            \'add_or_remove_items\'        => __( \'Add or remove items\', \'imagewize_portfolio_plugin\' ),
            \'choose_from_most_used\'      => __( \'Choose from the most used items\', \'imagewize_portfolio_plugin\' ),
            \'not_found\'                  => __( \'Not Found\', \'imagewize_portfolio_plugin\' ),
        );
        $rewrite = array(
            \'slug\'                       => \'label\',
            \'with_front\'                 => true,
            \'hierarchical\'               => true,
        );
        $args = array(
            \'labels\'                     => $labels,
            \'hierarchical\'               => true,
            \'public\'                     => true,
            \'show_ui\'                    => true,
            \'show_admin_column\'          => true,
            \'show_in_nav_menus\'          => true,
            \'show_tagcloud\'              => true,
            \'rewrite\'                    => $rewrite,
        );
        register_taxonomy( \'label\', array( \'Img_portfolio_cpt\' ), $args );

    }

    // Hook into the \'init\' action
    add_action( \'init\', \'portfolio_taxonomy\', 0 );



    if ( ! function_exists(\'img_portfolio_cpt\') ) {

    // Register Custom Post Type
    function img_portfolio_cpt() {

        $labels = array(
            \'name\'                => _x( \'Portfolio Items\', \'Post Type General Name\', \'imagewize_portfolio_plugin\' ),
            \'singular_name\'       => _x( \'Portfolio\', \'Post Type Singular Name\', \'imagewize_portfolio_plugin\' ),
            \'menu_name\'           => __( \'Portfolio\', \'imagewize_portfolio_plugin\' ),
            \'parent_item_colon\'   => __( \'Parent Item:\', \'imagewize_portfolio_plugin\' ),
            \'all_items\'           => __( \'All Items\', \'imagewize_portfolio_plugin\' ),
            \'view_item\'           => __( \'View Item\', \'imagewize_portfolio_plugin\' ),
            \'add_new_item\'        => __( \'Add New Item\', \'imagewize_portfolio_plugin\' ),
            \'add_new\'             => __( \'Add New\', \'imagewize_portfolio_plugin\' ),
            \'edit_item\'           => __( \'Edit Item\', \'imagewize_portfolio_plugin\' ),
            \'update_item\'         => __( \'Update Item\', \'imagewize_portfolio_plugin\' ),
            \'search_items\'        => __( \'Search Item\', \'imagewize_portfolio_plugin\' ),
            \'not_found\'           => __( \'Not found\', \'imagewize_portfolio_plugin\' ),
            \'not_found_in_trash\'  => __( \'Not found in Trash\', \'imagewize_portfolio_plugin\' ),
        );
        $rewrite = array(
            \'slug\'                => \'%label%\',
            \'with_front\'          => true,
            \'pages\'               => true,
            \'feeds\'               => true,
        );
        $args = array(
            \'label\'               => __( \'Img_portfolio_cpt\', \'imagewize_portfolio_plugin\' ),
            \'description\'         => __( \'Imagewize Portfolio\', \'imagewize_portfolio_plugin\' ),
            \'labels\'              => $labels,
            \'supports\'            => array( \'title\', \'editor\', \'author\', \'thumbnail\', ),
            \'taxonomies\'          => array( \'label\' ),
            \'hierarchical\'        => true,
            \'public\'              => true,
            \'show_ui\'             => true,
            \'show_in_menu\'        => true,
            \'show_in_nav_menus\'   => true,
            \'show_in_admin_bar\'   => true,
            \'menu_position\'       => 5,
            \'menu_icon\'           => \'dashicons-portfolio\',
            \'can_export\'          => true,
            \'has_archive\'         => true,
            \'exclude_from_search\' => false,
            \'publicly_queryable\'  => true,
            \'rewrite\'             => $rewrite,
            \'capability_type\'     => \'page\',
        );
        register_post_type( \'Img_portfolio_cpt\', $args );

    }

    // Hook into the \'init\' action
    add_action( \'init\', \'img_portfolio_cpt\', 0 );

    }
add_filter(\'post_type_link\', \'portfolio_permalink_structure\', 10, 4);
function portfolio_permalink_structure($post_link, $post, $leavename, $sample)
{
    if ( false !== strpos( $post_link, \'%label%\' ) ) {
        $label_term = get_the_terms( $post->ID, \'label\' );
        $post_link = str_replace( \'%label%\', array_pop( $label_term )->slug, $post_link );
    }
    return $post_link;
}
如@Pieter Goosen所述,必须检查CPT的命名问题。我还需要学习更多关于自定义帖子类型的知识,但今天确实学到了很多,并实现了目标。

SO网友:Pieter Goosen

这可能会解决您的问题,坦率地说,重写对我来说仍然有点不确定,因此我无法帮助您解决这一问题,但您的代码中有两个主要问题,在您尝试整理重写之前,应该先整理好

自定义post类型名称(以及就此而言,函数名称和自定义分类名称)应全部为小写字母和单词,并用下划线分隔。自定义邮件类型名称以大写字母开头。这可能会导致以后出现一些问题

你为分类法重写的代码是错误的。看一看。slug应该是小写的,单词应该用连字符分隔。您的重写段塞当前为Portfolio Category 这是行不通的。你得试试这样portfolio-category

进行这些更改,并记住在完成这些更改后刷新重写规则,否则这些更改将不会生效。更正此错误后,您可以开始查看重写问题

结束

相关推荐