Site icon Anthony Carbon

Add sub-page using 1 page without adding another pages using add_rewrite_rule

Add sub-page using 1 page without adding another pages using add_rewrite_rule is very simple but clean strategy most specially if you have multiple sub-pages.

Using add_rewrite_rule, your task is done. Please copy the codes to your functions.php.

Example:

I have this page, https://www.anthonycarbon.com/parent/ and the page ID is 10.


add_filter(‘query_vars’, ‘wdes_query_vars’);
function wdes_query_vars($vars) {
$vars[] = ‘parent_page’;
return $vars;
}
add_action( ‘init’, ‘application_sub_category_page’, 0);
function application_sub_category_page() {
add_rewrite_rule(‘^parent/([^/]*)/?’,’index.php?page_id=135&parent_page=$matches[1]’,’top’);
}

Output can be:

https://www.anthonycarbon.com/parent/my-child-page/
https://www.anthonycarbon.com/parent/any-sub-page-name/

Note: don’t forget to update permalinks after the functions.php is updated, else this will show page not found page.

Exit mobile version