What I'm trying to do is convert my one page design in wordpress and i thought it would be nice to be able to edit, add and modify different part of the page in seperated pages. The one page website will be ordered by a menu (with id main).
Following the wp-codex i used get_template_part
, it should work properly because it should:
Load a template part into a template (other than header, sidebar, footer)
get_header
gets skipped but get_footer
gets excecuted and the site is rendered incorrectly.
front-page.php
$pages = wp_get_nav_menu_items('main');
global $post;
foreach ($pages as $post) {
$post = get_post($post->object_id);
if($post->post_type != "page") continue;
setup_postdata( $post );
$postName = (locate_template('page-' . $post->post_name . '.php') == '') ? null : $post->post_name;
get_template_part('page', $postName);
}
wp_reset_postdata();
Is this a bug? Or did I do something wrong? What is the best way to accomplish this?
page.php
template "part"? The default WordPress page templates for as long as I can remember have included header, footer and even sidebar, so it's not really a "part" or partial template, but rather the whole thing. I'd say roll your own part, without callingget_header
,get_footer
, etc. – Tomas Butelerpage.php
, that usually containsget_{head,footer}()
. Also I wouldn't usepage-{slug}.php
, because it's used by WordPress to override the template for that particular page. I would use for exampleonepage-{slug}.php
. Try for example:if( ! is_null( $postName ) ) get_template_part('page', $postName);
to check if the layout is rendered correctly. – birgireget_header
,get_footer
andget_sidebar
but what it actually requires is a template without these functions. – Ties