5
votes

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?

4
What are the contents of your 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 calling get_header, get_footer, etc.Tomas Buteler
I agree with @tbuteler, make your own default template part, instead of using page.php, that usually contains get_{head,footer}(). Also I wouldn't use page-{slug}.php, because it's used by WordPress to override the template for that particular page. I would use for example onepage-{slug}.php. Try for example: if( ! is_null( $postName ) ) get_template_part('page', $postName); to check if the layout is rendered correctly.birgire
Ah so I interpreted it wrongly. In my (old) understanding wordpress would skip the functions get_header, get_footer and get_sidebar but what it actually requires is a template without these functions.Ties

4 Answers

0
votes

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.

0
votes

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 :

/** Get core data for a WP post/page using ID
* @param $id : wp post/page ID
* @return array
*/
function wp_get_single_post_basic($id){
    $post = get_post($id, ARRAY_A);
    $ret = array();
    $content = $post['post_content'];
    // FILTER via WP
    $content = apply_filters('the_content', $post['post_content']);
    $ret['title']= $post['post_title'];;
    $ret['content']= $content;
    $ret['link'] = $post['post_name'];
    $ret['edit_link'] = '<a href="'.get_edit_post_link($id).'" title="edit">edit</a>';
    return $ret;
}

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...

0
votes

I think you have a problem using the global $post and storing also the $post variable in the foreach 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, the reset_postdata and also the setup_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 a post_id, like the_permalink() or the_title().

0
votes

I ended up copying the default page.php template as page-page.php and loaded it like this:

$postName = (locate_template('page-' . $post->post_name . '.php') == '') ? 'page' : $post->post_name;

Then in page.php just loaded page-page.php