WordPress中的CPT网址中有多个类别

时间:2018-08-25 09:48:28

标签: wordpress custom-post-type permalinks

我有一个CPT Traffi咖啡厅。 网址:www.domain.com/traffic-offices /

添加新的www.domain.com/traffic-offices/germany/之类的按钮就可以了。 当我添加新帖子Saksen时,它将自动生成此URL www.domain.com/traffic-offices/saksen,但我想要www.domain.com/traffic-offices/germany/saksen。如果我自动将其填写为www.domain.com/traffic-offices/germany-saksen

如何获得此网址www.domain.com/traffic-offices/germany/saksen

    function trafficoffice_post_type() {

    // Labels
    $labels = array(
        'name' => _x("trafficoffices", "post type general name"),
        'singular_name' => _x("trafficoffices", "post type singular name"),
        'menu_name' => 'trafficoffices',
        'add_new' => _x("Add new", "trafficoffice item"),
        'add_new_item' => __("Add new"),
        'edit_item' => __("trafficoffice aanpassen"),
        'new_item' => __("Add new trafficoffice"),
        'view_item' => __("View trafficoffice"),
        'search_items' => __("Search Profiles"),
        'not_found' => __("No Profiles Found"),
        'not_found_in_trash' => __("No Profiles Found in Trash"),
        'parent_item_colon' => ''
    );

    // Register post type
    register_post_type('trafficoffice', array(
        'labels' => $labels,
        'public' => true,
        'has_archive' => false,
        'rewrite'     => array(
            'slug' => 'traffic-offices',
        ),
        'supports' => array('title', 'editor', 'thumbnail')
    ));
}

add_action('init', 'trafficoffice_post_type', 0);

1 个答案:

答案 0 :(得分:0)

一种方法是使用分类法。

  1. 注册一个称为“国家”的新分类法。
  2. 使用“德国”标签创建新的类别“德国”。
  3. 将类别“德国”分配给帖子“ Saksen”。

这是注册分类法的代码:

$labels = array(
    'name'                       => _x( 'Countries', 'taxonomy general name', 'textdomain' ),
    'singular_name'              => _x( 'Country', 'taxonomy singular name', 'textdomain' ),
    'search_items'               => __( 'Search Countries', 'textdomain' ),
    'popular_items'              => __( 'Popular Countries', 'textdomain' ),
    'all_items'                  => __( 'All Countries', 'textdomain' ),
    'parent_item'                => null,
    'parent_item_colon'          => null,
    'edit_item'                  => __( 'Edit Country', 'textdomain' ),
    'update_item'                => __( 'Update Country', 'textdomain' ),
    'add_new_item'               => __( 'Add New Country', 'textdomain' ),
    'new_item_name'              => __( 'New Country Name', 'textdomain' ),
    'separate_items_with_commas' => __( 'Separate Countries with commas', 'textdomain' ),
    'add_or_remove_items'        => __( 'Add or remove Countries', 'textdomain' ),
    'choose_from_most_used'      => __( 'Choose from the most used Countries', 'textdomain' ),
    'not_found'                  => __( 'No Countries found.', 'textdomain' ),
    'menu_name'                  => __( 'Countries', 'textdomain' ),
);

$args = array(
    'hierarchical'          => false,
    'labels'                => $labels,
    'show_ui'               => true,
    'show_admin_column'     => true,
    'update_count_callback' => '_update_post_term_count',
    'query_var'             => true,
    //'rewrite'               => array( 'slug' => 'country' ),
);

register_taxonomy( 'country', 'trafficoffice', $args );

确保WordPress的永久链接设置(设置->永久链接)包括类别。

/%category%/%postname%/

详细了解register_taxonomy()