permalinks - How can i have a custom post type with more slugs for each post?

I have a post type city ..lets say i create a city called toronto with the slug toronto-canada.now i have citytoron

I have a post type /city/ .. lets say i create a city called toronto with the slug toronto-canada.

now i have /city/toronto-canada/

is there a way i can have more paths for each post in the custom post type added dynamically?

for example when i make the toronto post there should be a path that is: /city/toronto-canada/stores and /city/toronto-canada/things-to-do

pretty much just a defined url for each post /city/toronto-canada/whatever

// City Post Type & Taxonomy
function create_city_category_hierarchical_taxonomy() {
  $labels = array(
    'name' => _x( 'Categories', 'taxonomy general name' ),
    'singular_name' => _x( 'Category', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Categories' ),
    'all_items' => __( 'All Categories' ),
    'parent_item' => __( 'Parent Category' ),
    'parent_item_colon' => __( 'Parent Category:' ),
    'edit_item' => __( 'Edit Category' ),
    'update_item' => __( 'Update Category' ),
    'add_new_item' => __( 'Add New Category' ),
    'new_item_name' => __( 'New Category Name' ),
    'menu_name' => __( 'Categories' ),
  );
  register_taxonomy('city_category',null, array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'cities'),
  ));
}
function city_post_type() {
  $labels = array(
    'name'               => _x( 'Cities', 'post type general name' ),
    'singular_name'      => _x( 'City', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New City' ),
    'edit_item'          => __( 'Edit City' ),
    'new_item'           => __( 'New City' ),
    'all_items'          => __( 'All Cities' ),
    'view_item'          => __( 'View Cities' ),
    'search_items'       => __( 'Search Cities' ),
    'not_found'          => __( 'No City found' ),
    'not_found_in_trash' => __( 'No Cities found in the Trash' ),
    'parent_item_colon'  => '',
    'menu_name'          => 'Cities'
  );

  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds xxx Cities',
    'public'        => true,
    'menu_position' => 99999,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'page-attributes','comments' ),
    'taxonomies'    => array('city_category'),
    'has_archive'   => true,
    'has_parent'    => true,
    'menu_icon'     => 'dashicons-building',
    'hierarchical'  => true,
    'rewrite' => array('slug' => 'city'),
  );
  register_post_type( 'city', $args );
  flush_rewrite_rules();
}
add_action( 'init', 'create_city_category_hierarchical_taxonomy', 0 );
add_action( 'init', 'city_post_type' );

Thanks!.

I have a post type /city/ .. lets say i create a city called toronto with the slug toronto-canada.

now i have /city/toronto-canada/

is there a way i can have more paths for each post in the custom post type added dynamically?

for example when i make the toronto post there should be a path that is: /city/toronto-canada/stores and /city/toronto-canada/things-to-do

pretty much just a defined url for each post /city/toronto-canada/whatever

// City Post Type & Taxonomy
function create_city_category_hierarchical_taxonomy() {
  $labels = array(
    'name' => _x( 'Categories', 'taxonomy general name' ),
    'singular_name' => _x( 'Category', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Categories' ),
    'all_items' => __( 'All Categories' ),
    'parent_item' => __( 'Parent Category' ),
    'parent_item_colon' => __( 'Parent Category:' ),
    'edit_item' => __( 'Edit Category' ),
    'update_item' => __( 'Update Category' ),
    'add_new_item' => __( 'Add New Category' ),
    'new_item_name' => __( 'New Category Name' ),
    'menu_name' => __( 'Categories' ),
  );
  register_taxonomy('city_category',null, array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'cities'),
  ));
}
function city_post_type() {
  $labels = array(
    'name'               => _x( 'Cities', 'post type general name' ),
    'singular_name'      => _x( 'City', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New City' ),
    'edit_item'          => __( 'Edit City' ),
    'new_item'           => __( 'New City' ),
    'all_items'          => __( 'All Cities' ),
    'view_item'          => __( 'View Cities' ),
    'search_items'       => __( 'Search Cities' ),
    'not_found'          => __( 'No City found' ),
    'not_found_in_trash' => __( 'No Cities found in the Trash' ),
    'parent_item_colon'  => '',
    'menu_name'          => 'Cities'
  );

  $args = array(
    'labels'        => $labels,
    'description'   => 'Holds xxx Cities',
    'public'        => true,
    'menu_position' => 99999,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'page-attributes','comments' ),
    'taxonomies'    => array('city_category'),
    'has_archive'   => true,
    'has_parent'    => true,
    'menu_icon'     => 'dashicons-building',
    'hierarchical'  => true,
    'rewrite' => array('slug' => 'city'),
  );
  register_post_type( 'city', $args );
  flush_rewrite_rules();
}
add_action( 'init', 'create_city_category_hierarchical_taxonomy', 0 );
add_action( 'init', 'city_post_type' );

Thanks!.

Share Improve this question asked Jul 29, 2019 at 17:47 Nickey22Nickey22 111 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

Yes you can do this with a custom rewrite. I am doing the same for a custom post type, in my case profile.

So my URL = domain/profile/some-custom-slug.
Dynamically I create /meet-me behind it, so it becomes domain/profile/some-custom-slug/meet-me.

The real address of the contact page is domain/profile/some-custom-slug/?meet=true (or something else unique).

function wpse343933_todo() {
    add_rewrite_rule( 'city/([^/]*)/(things-to-do)/?$', 'index.php?post_type=city&name=$matches[1]&subpage=$matches[2]', 'top' );
}
add_filter( 'init', 'wpse343933_todo', 1, 3 );

The code should be placed in functions.php.

Now we've only made the address 'reachable', but it shows the singular post.

In your singular.php you need to add an if statement to 'catch' this new variable.

The code for this is real easy:

if ( true == get_query_var( 'meet' ) {
    // output content for this particular 'sub page'
} else {
    // output content for the 'normal post'
}

I think this should do the trick.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745283700a4620431.html

相关推荐

  • permalinks - How can i have a custom post type with more slugs for each post?

    I have a post type city ..lets say i create a city called toronto with the slug toronto-canada.now i have citytoron

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信