admin管理员组文章数量:1130349
I have several pages set up in a hierarchy on my site. Currently the site's pages and URLs look like this.
Page name | Current URL | Preferred URL
-----------------|----------------------------------------
Page AAA | /pageAAA/ | /pageAAA/
|-- Page BBB | /pageAAA/pageBBB/ | /pageBBB/
Page CCC | /pageCCC/ | /pageCCC/
Page DDD | /pageDDD/ | /pageDDD/
|-- Page EEE | /pageDDD/pageEEE/ | /pageEEE/
I would like to strip the page hierarchy from the URL and just use the page name instead. Currently this is not a problem for the "POSTS" as I have set the "Permalink" to /%postname%/
Edit: The reason that I want to do this is; I am importing a existing site into wordpress. The Existing site has a hierarchy for the pages (menu) but a flat url structure. I do not know if this has any added benefit for SEO but I want to keep the URL structure the same as the old site
I have several pages set up in a hierarchy on my site. Currently the site's pages and URLs look like this.
Page name | Current URL | Preferred URL
-----------------|----------------------------------------
Page AAA | /pageAAA/ | /pageAAA/
|-- Page BBB | /pageAAA/pageBBB/ | /pageBBB/
Page CCC | /pageCCC/ | /pageCCC/
Page DDD | /pageDDD/ | /pageDDD/
|-- Page EEE | /pageDDD/pageEEE/ | /pageEEE/
I would like to strip the page hierarchy from the URL and just use the page name instead. Currently this is not a problem for the "POSTS" as I have set the "Permalink" to /%postname%/
Edit: The reason that I want to do this is; I am importing a existing site into wordpress. The Existing site has a hierarchy for the pages (menu) but a flat url structure. I do not know if this has any added benefit for SEO but I want to keep the URL structure the same as the old site
Share Improve this question edited Mar 21, 2013 at 18:42 Steven Smethurst asked Mar 21, 2013 at 18:14 Steven SmethurstSteven Smethurst 1514 bronze badges 8 | Show 3 more comments3 Answers
Reset to default 1I'd be curious if someone can find a better solution to this. Here's what I came up with:
function wpse_91821_flatten_page_paths( $wp ) {
if ( false !== strpos( $wp->matched_query, 'pagename=' ) && isset( $wp->query_vars['pagename'] ) && $wp->query_vars['pagename'] && false === strpos( $wp->query_vars['pagename'], '/' ) ) {
if ( !get_page_by_path( $wp->query_vars['pagename'] ) ) {
$page = get_posts( array(
'name' => $wp->query_vars['pagename'],
'post_type' => 'page',
'post_status' => 'publish',
'numberposts' => 1
) );
if ( $page && isset( $page[0] ) ) {
$wp->query_vars['pagename'] = get_page_uri( $page[0]->ID );
$wp->request = $wp->query_vars['pagename'];
}
}
}
}
add_action( 'parse_request', 'wpse_91821_flatten_page_paths', 5 );
What I'm doing here is intercepting parse_request and if it's a pagename request, and the pagename doesn't have a "/" in it, then I check to see if I can find a post with the correct name. If I find a page, I set the query var, which allows the rest of the request chain to proceed as normal, because WordPress thinks the request is the full hierarchical one.
You'd also want to add a filter to post_type_link so that your links are generated correctly (otherwise they'll continue to be hierarchical).
I wouldn't change page permalink behavior. Much simpler/safer solution will be to set flat page hierarchy (if you really want it to be flat).
If you want to have hierarchy in menu, you can still have it - you can create custom menu and display it with wp_nav_menu function.
function wpse_91821_flatten_page_paths( $wp ) {
if ( false !== strpos( $wp->matched_query, 'pagename=' ) && isset( $wp->query_vars['pagename'] ) && $wp->query_vars['pagename'] && false === strpos( $wp->query_vars['pagename'], '/' ) ) {
if ( !get_page_by_path( $wp->query_vars['pagename'] ) ) {
$page = get_posts( array(
'name' => $wp->query_vars['pagename'],
'post_type' => 'page',
'post_status' => 'publish',
'numberposts' => 1
) );
if ( $page && isset( $page[0] ) ) {
$wp->query_vars['pagename'] = get_page_uri( $page[0]->ID );
$wp->request = $wp->query_vars['pagename'];
}
}
}
}
add_action( 'parse_request', 'wpse_91821_flatten_page_paths', 5 );
function custom_permalinks_page_link( $permalink, $post_id ) {
if ( empty( $post_id ) ) return $permalink;
$post = get_post( $post_id );
return home_url( $post->post_name . "/");
}
add_filter( 'page_link', 'custom_permalinks_page_link', 10, 2 );
function custom_rewrite() {
add_rewrite_rule('^([^/]+)?', 'index.php?pagename= $matches[1]', 'top');
}
add_action( 'init', 'custom_rewrite' );
As of wp 4.7, you would need to work around the parse_request: as use_verbose_page_rules is triggered and get_page_by_path call checks the page parents and their presence in the url(!) - this unexpected core check could be avoided by adding an evil space as in https://github/weaveworks/wordepress/blob/master/plugin/wordepress/wordepress.php#L91
This is, of course, very fragile and should be probably avoided :)
This presentation may contain a better answer but I was not able to comprehend it: https://www.slideshare/MikeSchinkel/hardcore-url-routing-2014?next_slideshow=1
I have several pages set up in a hierarchy on my site. Currently the site's pages and URLs look like this.
Page name | Current URL | Preferred URL
-----------------|----------------------------------------
Page AAA | /pageAAA/ | /pageAAA/
|-- Page BBB | /pageAAA/pageBBB/ | /pageBBB/
Page CCC | /pageCCC/ | /pageCCC/
Page DDD | /pageDDD/ | /pageDDD/
|-- Page EEE | /pageDDD/pageEEE/ | /pageEEE/
I would like to strip the page hierarchy from the URL and just use the page name instead. Currently this is not a problem for the "POSTS" as I have set the "Permalink" to /%postname%/
Edit: The reason that I want to do this is; I am importing a existing site into wordpress. The Existing site has a hierarchy for the pages (menu) but a flat url structure. I do not know if this has any added benefit for SEO but I want to keep the URL structure the same as the old site
I have several pages set up in a hierarchy on my site. Currently the site's pages and URLs look like this.
Page name | Current URL | Preferred URL
-----------------|----------------------------------------
Page AAA | /pageAAA/ | /pageAAA/
|-- Page BBB | /pageAAA/pageBBB/ | /pageBBB/
Page CCC | /pageCCC/ | /pageCCC/
Page DDD | /pageDDD/ | /pageDDD/
|-- Page EEE | /pageDDD/pageEEE/ | /pageEEE/
I would like to strip the page hierarchy from the URL and just use the page name instead. Currently this is not a problem for the "POSTS" as I have set the "Permalink" to /%postname%/
Edit: The reason that I want to do this is; I am importing a existing site into wordpress. The Existing site has a hierarchy for the pages (menu) but a flat url structure. I do not know if this has any added benefit for SEO but I want to keep the URL structure the same as the old site
Share Improve this question edited Mar 21, 2013 at 18:42 Steven Smethurst asked Mar 21, 2013 at 18:14 Steven SmethurstSteven Smethurst 1514 bronze badges 8-
Is there any reason to do this? From SEO point of view it could be the plus.
example/Kodak/Kodak-1234/is OK. Can you precise, please? – Max Yudin Commented Mar 21, 2013 at 18:32 - 1 Why create a hierarchy at all, then? – vancoder Commented Mar 21, 2013 at 18:38
- @MaxYudin I do not know if there is a added SEO benefit, – Steven Smethurst Commented Mar 21, 2013 at 18:42
- @vancoder I want the hierarchy for the menu system in wordpress. – Steven Smethurst Commented Mar 21, 2013 at 18:43
- You can create your own hierarchy in the menu system...? It's independent of parent/child pages. – vancoder Commented Mar 21, 2013 at 18:44
3 Answers
Reset to default 1I'd be curious if someone can find a better solution to this. Here's what I came up with:
function wpse_91821_flatten_page_paths( $wp ) {
if ( false !== strpos( $wp->matched_query, 'pagename=' ) && isset( $wp->query_vars['pagename'] ) && $wp->query_vars['pagename'] && false === strpos( $wp->query_vars['pagename'], '/' ) ) {
if ( !get_page_by_path( $wp->query_vars['pagename'] ) ) {
$page = get_posts( array(
'name' => $wp->query_vars['pagename'],
'post_type' => 'page',
'post_status' => 'publish',
'numberposts' => 1
) );
if ( $page && isset( $page[0] ) ) {
$wp->query_vars['pagename'] = get_page_uri( $page[0]->ID );
$wp->request = $wp->query_vars['pagename'];
}
}
}
}
add_action( 'parse_request', 'wpse_91821_flatten_page_paths', 5 );
What I'm doing here is intercepting parse_request and if it's a pagename request, and the pagename doesn't have a "/" in it, then I check to see if I can find a post with the correct name. If I find a page, I set the query var, which allows the rest of the request chain to proceed as normal, because WordPress thinks the request is the full hierarchical one.
You'd also want to add a filter to post_type_link so that your links are generated correctly (otherwise they'll continue to be hierarchical).
I wouldn't change page permalink behavior. Much simpler/safer solution will be to set flat page hierarchy (if you really want it to be flat).
If you want to have hierarchy in menu, you can still have it - you can create custom menu and display it with wp_nav_menu function.
function wpse_91821_flatten_page_paths( $wp ) {
if ( false !== strpos( $wp->matched_query, 'pagename=' ) && isset( $wp->query_vars['pagename'] ) && $wp->query_vars['pagename'] && false === strpos( $wp->query_vars['pagename'], '/' ) ) {
if ( !get_page_by_path( $wp->query_vars['pagename'] ) ) {
$page = get_posts( array(
'name' => $wp->query_vars['pagename'],
'post_type' => 'page',
'post_status' => 'publish',
'numberposts' => 1
) );
if ( $page && isset( $page[0] ) ) {
$wp->query_vars['pagename'] = get_page_uri( $page[0]->ID );
$wp->request = $wp->query_vars['pagename'];
}
}
}
}
add_action( 'parse_request', 'wpse_91821_flatten_page_paths', 5 );
function custom_permalinks_page_link( $permalink, $post_id ) {
if ( empty( $post_id ) ) return $permalink;
$post = get_post( $post_id );
return home_url( $post->post_name . "/");
}
add_filter( 'page_link', 'custom_permalinks_page_link', 10, 2 );
function custom_rewrite() {
add_rewrite_rule('^([^/]+)?', 'index.php?pagename= $matches[1]', 'top');
}
add_action( 'init', 'custom_rewrite' );
As of wp 4.7, you would need to work around the parse_request: as use_verbose_page_rules is triggered and get_page_by_path call checks the page parents and their presence in the url(!) - this unexpected core check could be avoided by adding an evil space as in https://github/weaveworks/wordepress/blob/master/plugin/wordepress/wordepress.php#L91
This is, of course, very fragile and should be probably avoided :)
This presentation may contain a better answer but I was not able to comprehend it: https://www.slideshare/MikeSchinkel/hardcore-url-routing-2014?next_slideshow=1
本文标签: Removing hierarchical pages in the permalink
版权声明:本文标题:Removing hierarchical pages in the permalink 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749056094a2309366.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


example/Kodak/Kodak-1234/is OK. Can you precise, please? – Max Yudin Commented Mar 21, 2013 at 18:32