[Subscribe]

Edit sidebar content as a page in WordPress

By Colin Temple on 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.

Posted in Blogging, Wordpress and tagged as , .

8 Comments on “Edit sidebar content as a page in WordPress”

  1. Robin | August 11th, 2008 at 10:10 pm

    Can you tell us what you entered into query.php file?

  2. Colin Temple | August 11th, 2008 at 10:14 pm

    Nothing. I just put that snippet of code into a sidebar widget (which I created via a plugin).

    You could put that code into any template file. It doesn’t modify WP_Query, it just creates a new instance of it. All the code really does is output the content of a specific page (by slug).

  3. Kevin | June 15th, 2009 at 11:30 am

    Thanks for this, it’s very helpful. Someday I would love to see the ability to manage sidebar specific content on a page by page basis in WordPress… but haven’t seen anything out there that can handle it just yet.

    Hopefully in the future!

  4. Colin Temple | June 15th, 2009 at 11:36 am

    For the moment, you’d have to either make a template file for each page that you wanted a unique sidebar for, or modify the “Page” template to pass some identifier to your dynamic_sidebar() function.

    Either way, you’d also have to initiate dynamic sidebars for each page.

    It would be neat to have a “Use this sidebar:” with a list of existing ones, and an option to make a new sidebar for the page within the UI.

  5. Cynthia | January 17th, 2010 at 9:02 pm

    There is a great plug-in called “Display Widgets” that allows you to choose which page your widget shows up on. In combination with the above, it should be quite powerful!

  6. Connor MacDonald | August 1st, 2010 at 5:55 pm

    Hi, Colin–

    Why do I get WP_Query errors on any of the scripts I try to use WP_Query on, regardless of what theme I use?

    Best Regards

  7. Colin Temple | August 2nd, 2010 at 7:21 pm

    I’m not sure, Connor. What’s the error you’re getting.

    One thing that may be an issue — WordPress changes my code here in the snippet above. My quotes before and after the pagename query are transformed to the fancy before and after “curly” quotes – rsquo and lsquo. You’ll want to replace them with straight quotes from your keyboard: “

  8. Colin Temple | August 2nd, 2010 at 7:22 pm

    (See, even my comments are being fooled again… but you get the idea. A normal double-quote rather than a curly one.)

Leave a Reply