Skip to content

Instantly share code, notes, and snippets.

@sueharaluke
Last active January 5, 2021 09:39
Show Gist options
  • Save sueharaluke/89a5721950286be7d1450376d1b0e1ef to your computer and use it in GitHub Desktop.
Save sueharaluke/89a5721950286be7d1450376d1b0e1ef to your computer and use it in GitHub Desktop.
Organize wordpress pages files in folders
<?php
/*
This is by no means an elegant solution. (Maybe someone can elegantly loop it.)
With this page.php in place, you can store your custom template pages inside folders.
ie.:
/company/ -> pages/page-company.php
/company/about/ -> pages/page-about.php
/company/about/partners/ -> pages/about/page-partners.php
Better them storing them flatly inside the theme folder.
*/
$parent = get_post($post->post_parent);
$grand_parent_id = $parent->post_parent;
$folder_path = 'pages/';
// 2nd and 3rd level pages (most pages are here)
if($grand_parent_id == 0) {
$folder_path .= $parent->post_name;
} else {
// 4th level
$folder_path .= get_post($grand_parent_id)->post_name.'/'.$parent->post_name;
}
// 5th and beyond ? You're on your own.
get_template_part($folder_path.'/page', $pagename);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment