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:
<?php
// 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.
Just what i needed. Thanks a lot!
You’re very welcome. Always happy to hear that I’ve posted something helpful.
Just what I’m looking for. Thanks a lot!
This made it a little less complicated. Thanks!
Saved my ARSE! I thought I would never figure out how to do this!
Thanks a million times over!!! 🙂
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!
Thank you for sharing this great tutorial.
Just what I needed 🙂
novice at this stuff where do i place this code
You should be able to put it in any of your theme files. Just stick it wherever you’d like the page content to show up.
Thanks… this is what i need.
Thank you so much! Ah, I love when Google brings me to a nice and simple explanation like this.