permalinks - Custom post type as home page with custom url

admin2025-06-07  23

I've changed the permalinks to add a string at the beginning of the url like this: /london/%postname%/ This works perfect for all the pages but not for my home page.

I set my home page to be a single custom post type in settings->reading->static page so the url was like this:

But now it's:

How can i change it so the home page is just the single custom post type without the "/destination/london" part?

Edit:

After adding this code it almost work:

function enable_front_page_destination( $query ){
    if('' == $query->query_vars['post_type'] && 0 != $query->query_vars['page_id'])
        $query->query_vars['post_type'] = array( 'page', 'destination' );
}
add_action( 'pre_get_posts', 'enable_front_page_destination' );

But now the home url is / and it should be

Thank you.

I've changed the permalinks to add a string at the beginning of the url like this: /london/%postname%/ This works perfect for all the pages but not for my home page.

I set my home page to be a single custom post type in settings->reading->static page so the url was like this: http://example/destination/london

But now it's: http://example/london/destination/london

How can i change it so the home page is just the single custom post type without the "/destination/london" part?

Edit:

After adding this code it almost work:

function enable_front_page_destination( $query ){
    if('' == $query->query_vars['post_type'] && 0 != $query->query_vars['page_id'])
        $query->query_vars['post_type'] = array( 'page', 'destination' );
}
add_action( 'pre_get_posts', 'enable_front_page_destination' );

But now the home url is http://example/ and it should be http://example/london

Thank you.

Share Improve this question edited Oct 10, 2018 at 9:32 Oterox asked Oct 10, 2018 at 9:04 OteroxOterox 3521 gold badge4 silver badges13 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

EDIT Since, as you mentioned it in comments, it can have several values, maybe we could retrieve the city list and set corresponding rewrite rules like so?

assuming $city_list is an array of cities

foreach ($city_list as $city) {
    add_rewrite_rule('/^'. $city .'/destination/'. $city .'$/','index.php?p=idofyourhomepage','top');
}

How about trying some rewrite rules.

add_rewrite_rule('/^london/destination/london$/','index.php?p=idofyourhomepage','top');

Or

add_rewrite_rule('/^london/destination/london$/','index.php?pagename=nameofyourhomepage','top');

Go validate your permalink settings to refresh the rules.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749255291a317655.html

最新回复(0)