使用CPT和分层分类的自定义固定链接

时间:2017-08-24 作者:vajrasar

CPT:产品分类法:prod\\U cat

当前URL

子术语domain.com/product-category/pulp/domain.com/product-category/pulp-iqf/pulp/apple/domain.com/product/apple-juice-concentrate/所需URL

子术语domain.com/pulp/domain.com/pulp-iqf/pulp/apple/domain.com/pulp-iqf/pulp/apple/apple-juice-concentrate/我已经深入了解了WPSE的答案,相信我,我尝试了各种组合,但我无法这样设置。关于创建您自己的规则的充满regex的解决方案让我感到困惑,因此如果有人能够解释如何实现所需的URL结构或如何实现接近所需URL的内容,我将不胜感激。

//Register product post type
function products_post_type() {

        $labels = array(
            \'name\'                  => \'Products\',
            \'singular_name\'         => \'Product\',
            \'menu_name\'             => \'Products\',
            \'name_admin_bar\'        => \'Post Type\',
            \'parent_item_colon\'     => \'Parent Product:\',
            \'all_items\'             => \'All Products\',
            \'add_new_item\'          => \'Add New Product\',
            \'add_new\'               => \'New Product\',
            \'new_item\'              => \'New Item\',
            \'edit_item\'             => \'Edit Product\',
            \'update_item\'           => \'Update Product\',
            \'view_item\'             => \'View Product\',
            \'search_items\'          => \'Search products\',
            \'not_found\'             => \'No products found\',
            \'not_found_in_trash\'    => \'No products found in Trash\',
            \'items_list\'            => \'Items list\',
            \'items_list_navigation\' => \'Items list navigation\',
            \'filter_items_list\'     => \'Filter items list\',
        );
        $args = array(
            \'label\'                 => \'Product\',
            \'description\'           => \'Products Post Type\',
            \'labels\'                => $labels,
            \'supports\'              => array( \'title\', \'editor\', \'excerpt\', \'thumbnail\', \'custom-fields\', ),
            \'hierarchical\'          => true,
            \'public\'                => true,
            \'show_ui\'               => true,
            \'show_in_menu\'          => true,
            \'menu_position\'         => 5,
            \'show_in_admin_bar\'     => true,
            \'show_in_nav_menus\'     => true,
            \'can_export\'            => true,
            \'has_archive\'           => true,        
            \'exclude_from_search\'   => false,
            \'publicly_queryable\'    => true,
            \'capability_type\'       => \'post\',
            \'rewrite\'               => array(
                                        \'slug\' => \'product\',
                                        \'hierarchical\' => true,
                                        \'with_front\' => false,
                                        )
        );
        register_post_type( \'product\', $args );

    }
    add_action( \'init\', \'products_post_type\', 0 );

    // Register Product Category
    function prod_cat() {

        $labels = array(
            \'name\'                       => \'Product Categories\',
            \'singular_name\'              => \'Product Category\',
            \'menu_name\'                  => \'Product Category\',
            \'all_items\'                  => \'All Items\',
            \'parent_item\'                => \'Parent Item\',
            \'parent_item_colon\'          => \'Parent Item:\',
            \'new_item_name\'              => \'New Item Name\',
            \'add_new_item\'               => \'Add New Item\',
            \'edit_item\'                  => \'Edit Item\',
            \'update_item\'                => \'Update Item\',
            \'view_item\'                  => \'View Item\',
            \'separate_items_with_commas\' => \'Separate items with commas\',
            \'add_or_remove_items\'        => \'Add or remove items\',
            \'choose_from_most_used\'      => \'Choose from the most used\',
            \'popular_items\'              => \'Popular Items\',
            \'search_items\'               => \'Search Items\',
            \'not_found\'                  => \'Not Found\',
            \'items_list\'                 => \'Items list\',
            \'items_list_navigation\'      => \'Items list navigation\',
        );
        $args = array(
            \'labels\'                        => $labels,
            \'hierarchical\'                  => true,
            \'public\'                        => true,
            \'show_ui\'                       => true,
            \'show_admin_column\'             => true,
            \'show_in_nav_menus\'             => true,
            \'show_tagcloud\'                 => true,
            \'rewrite\'                       => array(   \'slug\' => \'product-category\',
                                                        \'hierarchical\' => true,
                                                        \'with_front\' => false, 
                                                    )  
        );
        register_taxonomy( \'prod_cat\', array( \'product\' ), $args );

    }
    add_action( \'init\', \'prod_cat\', 0 );

1 个回复
SO网友:Milan Petrovic

CPT没有太多用于构建自定义永久链接的选项。如果没有自定义编码、创建新的重写规则和处理永久链接解析,您就无法完成所需的工作。

我有一个插件用于处理帖子类型和分类法,它包括定制永久链接等选项。这不是一个免费的插件,但在许多方面都是独特的,你可以通过我的个人资料主页查看它。

结束

相关推荐

Change Taxonomy Permalinks

我有自定义帖子,我创建了一个显示所有自定义帖子的页面。示例:www.example.com/archive-page我想知道是否可以更改与此自定义帖子相关的类别和标签的永久链接。现在我有:www.example.com/my-custom-post-type-cats/my-category-1www.example.com/my-custom-post-type-tags/my-tag-1</我想要这样的东西:www.example.com/archive-page?category=1www.e