One page wordpress issue with get_template_part and get_footer

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:

Read More

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?

Related posts

Leave a Reply

4 comments

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

  2. 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().

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