组合类别和自定义分类

时间:2017-12-04 作者:Niels

我想将类别URL与分类法结合起来。

例如,现在URL是:domain.com/category/?district=citycentre我想使此URL如下所示:domain.com/category/citycentre

这可能吗?

1 个回复
SO网友:kierzniak

对于以下职位类型和分类:

/**
 * Register restaurant post type
 */
function wpse_287682_register_restaurant_post_type() {

    $labels = array(
        \'name\' => __( \'Restaurants\' ),
        \'singular_name\' => __( \'Restaurant\' ),
        \'add_new\' => __( \'Add new\' ),
        \'add_new_item\' => __( \'Add new\' ),
        \'edit_item\' => __( \'Edit\' ),
        \'new_item\' => __( \'New\' ),
        \'view_item\' => __( \'View\' ),
        \'search_items\' => __( \'Search\' ),
        \'not_found\' => __( \'Not found\' ),
        \'not_found_in_trash\' => __( \'Not found Restaurants in trash\' ),
        \'parent_item_colon\' => __( \'Parent\' ),
        \'menu_name\' => __( \'Restaurants\' ),

    );

    $args = array(
        \'labels\' => $labels,
        \'hierarchical\' => false,
        \'supports\' => array( \'title\', \'editor\' ),
        \'taxonomies\' => array(),
        \'public\' => true,
        \'show_ui\' => true,
        \'show_in_menu\' => true,
        \'show_in_nav_menus\' => false,
        \'publicly_queryable\' => true,
        \'exclude_from_search\' => true,
        \'has_archive\' => true,
        \'query_var\' => true,
        \'can_export\' => true,
        \'rewrite\' => array( \'slug\' => \'restaurant\' ),
        \'capability_type\' => \'post\',
    );

    register_post_type( \'restaurant\', $args );
}

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

/**
 * Register district taxonomy
 */
function wpse_287682_register_district_taxonomy() {

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

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

    register_taxonomy( \'district\', array( \'restaurant\' ), $args );
}

add_action( \'init\', \'wpse_287682_register_district_taxonomy\', 0 );
您可以添加这种重写规则。

/**
 * Add custom rewrite rule for restaurant post type and district taxonomy.
 *
 * Remember to flush rewrite rules to apply changes.
 */
function wpse_287682_add_restaurant_district_rewrite_rule() {
    add_rewrite_rule(\'restaurant/district/([^/]+)/?$\', \'index.php?post_type=restaurant&district=$matches[1]\', \'top\');
}

add_action(\'init\', \'wpse_287682_add_restaurant_district_rewrite_rule\');
您的城市中心区链接将是:http://example.com/restaurant/district/citycenter/.

记住在代码更新后刷新重写规则。

结束

相关推荐

Taxonomy Relationships

我创建了两种分类法:制造车型这是针对CPT车辆的。不用说,品牌就是你的品牌(本田/丰田/福特等),然后你会根据品牌选择车型。如何设置Make和Model之间的关系?或者我在这方面完全错了吗?事实上,这应该是一个单一的分类法“Make&;型号\'?我不是在寻找:Vehicle CPT |-> Make |-> Model 我正在寻找:Vehicle CPT |-> Make |--> Model