Colin Temple

Writing Projects CV

Twitter LinkedIn Twitter Academia Last.fm RSS

Edit sidebar content as a page in WordPress

August 11, 2008

I recently came across a situation where I needed to display the contents of a page in a WordPress sidebar.  Essentially, my client needed an easy way to edit a sidebar that appeared on multiple pages.  Editing a Text widget wasn’t an option: they needed the flexibility of editing a page.

It took a few tries/revisions to find the best way to do this, so I thought I’d share my solution.  All I did was create a page with a specific slug (“sidebarpage”) and echo its content.  I used it in a sidebar widget, but you could use this anywhere in your WordPress Theme if you’d like to have an additional editable area.

<?php
$sidebarpage = new WP_Query(“pagename=sidebarpage”);
while($sidebarpage->have_posts()) : $sidebarpage->the_post();
the_content();
endwhile;
?>

Be sure that you’re creating a seperate WP_Query object ($sidebarpage in this example) — otherwise you’ll modify the query on the page itself, and your sidebar content will show up in the main body of your posts/pages as well!

The down-side of having this content stored in a page is that the page will appear in page listings, so be sure to exclude it if these are used in your theme.

I'm Colin and I'm a Senior Analyst at Napkyn Inc, leading a team of business analysts who support the decision-makers in major online retailers, software companies and sales organizations.

I'm also a philosophy student and teaching assistant at the University of Ottawa. I'm interested in philosophical logic, as well as philosophy of logic, science and mathematics.

Opinions expressed here may not reflect those of my employers or my school.

Copyright © 2008–2012 Colin David Temple