Load A Page Into A WordPress Theme Outside Of The Loop

I build a fair number of WordPress sites on small budgets for my employer, Union Street Media. Sometimes I need to give our clients an editable region in the sidebar, away from the main blog/page content. Here’s a quick and dirty trick I use to pull a page into a theme outside of the loop:

// must use a variable for page id
// http://codex.wordpress.org/Function_Reference/get_page
$id = 3;
$p = get_page($id);
echo apply_filters('the_content', $p->post_content);

You can get your page ID by editing the page. It will show up in the url (the ‘post=x’ portion.) It’s important to note that you have to pass a variable to the get_page() function. If you just pass an integer it will throw a fatal error. No need to go into why that is; just keep it in mind. get_page() essentially wraps get_post() so that function’s documentation is a good place to start if you want to learn what’s available to you.

This isn’t a particularly pretty solution, but it’s quick and it works well provided you know your page IDs and you’re not making a theme for distribution. I like to name my page something like ‘**Sidebar Content’ so it is easy to differentiate from regular pages.

12 thoughts on “Load A Page Into A WordPress Theme Outside Of The Loop

  1. Hey,

    This is the one that I use to display content outside of the loop:

    $page_id = get_page_by_title($page->page_title);
    $page_data = get_page($page_id);
    echo $page_data->post_content;

    I find it handy because you can use $page_id when you’re getting your dynamic sidebars too — if you’ve given them the same name as your page titles that is!

Comments are closed.