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?
if your get_template_part() is not working, then the problem may be there is no file named as page.php in theme, check for this file.
For what it’s worth, I often skip a some of the WP helpers and work more directly, but not rogue. Here is a snippet from my library :
That function breaks down the content in a very easy to manage manner and generates the edit link (build bool for that in calling script). Also formats content.
So grab and sort the IDs you want and run this in the loop over IDs for your one page. Then all of the content will be isolated by page/post/whatever (custom taxonomy FTW).
If you had the IDs ready and sorted plus the html/css ready to go I could drop this function in and work your one page to complete in under an hour. For production line work this type of helper is great — also perfect for sites where you want a specific post/page to be placed in some weird way in some weird place outside of the loop.
If you have a problem w/script let me know, I have not used it in a few months, wrote it a few years ago…
I think you have a problem using the global
$post
and storing also the$post
variable in theforeach
loop from the menu objects. I think probably that’s the cause of the error with the template part calling.I would recommend you to remove the global
$post
declaration, thereset_postdata
and also thesetup_postdata
since you aren’t using the global in your loop, you’re just naming it.You’re getting the
$post
object from the menu so it appears that you don’t need the global or the other functions that are mostly used when you want to use “Loop Style” template tags without apost_id
, likethe_permalink()
orthe_title()
.I ended up copying the default page.php template as
page-page.php
and loaded it like this:Then in
page.php
just loadedpage-page.php