1

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!.

1 Answer 1

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.com/profile/some-custom-slug.
Dynamically I create /meet-me behind it, so it becomes domain.com/profile/some-custom-slug/meet-me.

The real address of the contact page is domain.com/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.

3
  • Wouldn't this mean that the new sub page will not be crawled from google? is there a way to make a template for the sub page that google would be able to crawl instead?
    – Nickey22
    Commented Jul 30, 2019 at 16:13
  • Or would google crawl both the city/single/ and city/single/things-to-do seperately?
    – Nickey22
    Commented Jul 30, 2019 at 16:41
  • I think that, if you link to it from a page, Google will notice the link and crawl it, but I am not 100% sure.
    – Beee
    Commented Jul 30, 2019 at 19:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.