Last active
August 29, 2015 14:19
-
-
Save willybahuaud/e99063b6c8bb5253b342 to your computer and use it in GitHub Desktop.
Set childpage url without parnt slug
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| add_action('page_link', 'willy_page_link', 10, 3); | |
| function willy_page_link( $link, $post_id, $sample ) { | |
| $post = get_post( $post_id ); | |
| if ( 'page' == get_option( 'show_on_front' ) | |
| && $post->ID == get_option( 'page_on_front' ) ) { | |
| $link = home_url('/'); | |
| } else { | |
| $draft_or_pending = in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ); | |
| if ( ( isset( $post->post_status ) && ! $draft_or_pending ) || $sample ) { | |
| $link = user_trailingslashit( home_url( $post->post_name ), 'page' ); | |
| } else { | |
| $link = home_url( '?page_id=' . $post->ID ); | |
| } | |
| } | |
| return $link; | |
| } | |
| add_filter('query_vars','new_vars'); | |
| function new_vars($vars){ | |
| $vars[] = 'childpage'; | |
| return $vars; | |
| } | |
| add_filter('rewrite_rules_array', 'wabeo_rewrite_rules'); | |
| function wabeo_rewrite_rules( $rules ) { | |
| $a = array( '([^/]+)/?$' => 'index.php?childpage=$matches[1]'); | |
| $b = array( '.?.+?/([^/]+)(/[0-9]+)/?$' => 'index.php?childpage=$matches[1]&paged=$matches[2]'); | |
| $aRules = $a + $b + $rules; | |
| return $aRules; | |
| } | |
| add_filter( 'pre_get_posts', 'willy_find_child_page' ); | |
| function willy_find_child_page( $q ) { | |
| if ( ! is_admin() | |
| && $q->is_main_query() | |
| && isset( $q->query_vars['childpage'] ) ) { | |
| $q->set( 'name', sanitize_title( $q->query_vars['childpage'] ) ); | |
| $q->set( 'post_type', 'page' ); | |
| } | |
| return $q; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment