简体   繁体   中英

Custom URL structure in Wordpress

I have 3 custom post types:

development house_type room I have used ACF to build some relationships between these custom post types so development sits at the top and can have many house_types underneath it. Then a house_type can have many rooms beneath it.

I want to modify the url structure so that it is hierarchical also so that the user can go to /{development} which will display the single development page listing all the house_types at the development. Then if they click a housetype the url will be /{development}/{house_type} and will display the single house_type page which will list all of the rooms within that house type. Then finally if they click a room the url will be /{development}/{house_type}/{room} and will display the single room page with information about that room.

I have tried quite a few things but not managed to achieve this yet. The following works to a certain extent but not perfectly as it performs redirects. Like if I go to /{development}/{house_type} it redirects to /house-types/{house-type} which is the right page but I don't want the URL to change. Also if I go to /{development}/{house_type}/{room} the URL is correct but it displays the single development page instead of the single room page.

function my_rewrite_rules() {

    flush_rewrite_rules();

    add_rewrite_tag( '%development%', '([^&]+)' );
    add_rewrite_tag( '%room%', '([^&]+)' );
    add_rewrite_tag( '%house-type%', '([^&]+)' );

    add_rewrite_rule( '^%development%/%room%/([^/]*)?', 'index.php?room=$matches[1]' );
    add_rewrite_rule( '^%development%/([^/]*)?', 'index.php?house_type=$matches[1]' );
    add_rewrite_rule( '^([^/]*)/?', 'index.php?post_type=development&development=$matches[1]' );
}
add_action( 'init', 'my_rewrite_rules' );

What am I doing wrong?

Sorted in the end with the following:

/**
* Custom rewrite rules
*/
function my_rewrite_rules() {
    add_rewrite_rule( '^([^/]*)/([^/]*)/([^/]*)?', 'index.php?post_type=room&room=$matches[3]', 'top' );
    add_rewrite_rule( '^([^/]*)/([^/]*)?', 'index.php?post_type=house_type&house_type=$matches[2]', 'top' );
    add_rewrite_rule( '^([^/]*)/?', 'index.php?post_type=development&development=$matches[1]', 'top' );
}
add_action( 'init', 'my_rewrite_rules' );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM