Home Page Sidebar

As I’m using a static page for the home page, I had to modify the settings for that under Settings->Reading in the WP admin interface. The home page will then show the content from the specified page.

However, I want a different sidebar for this page. Instead of showing more navigation links, it should show the schedule of church activities. On the previous site I did, I hardcoded the whole schedule right into the page template file and set it as the page template. This means that when changes are made to the schedule, the template file itself has to be edited, a troublesome procedure. What if I can load a normal page’s content as sidebar content?

Displaying a Different Sidebar

The get_sidebar() function allows different sidebar template files to be called. Since the whole idea is to use a different sidebar for the home page, I changed the sidebar code in the home page template file to get_sidebar('home'). This will use the file sidebar-home.php as the sidebar.

Creating the Sidebar Template

I modified the sidebar.php file from the original theme. I want the WordPress Loop to just display the page which holds the sidebar content.

query_posts ('pagename=home-sidebar');
while(have_posts()) {
	the_post();
	the_content();
}
wp_reset_query();

The key is to specify the page name in query_posts(). It’s also possible to reference it by post ID, but using the name will be more flexible when I recreate this site on the actual server.

Note that the_post() is necessary to advance to the next post, or an infinite loop will results. The actual function which displays content is the_content. It seems very obvious now, but before I looked it up I was wondering why the_post() wasn’t giving any output.

Getting Rid of Auto Paragraphs

My custom sidebar content was filled with HTML because I needed to put everything in a table surrounded by <div> elements so I could style them in CSS. There was way too much space between lines because the WordPress editor automatically adds &ltp&gt tags around the lines. To prevent this, I added the line

remove_filter('the_content','wpautop');

to my sidebar-home.php file. This stops WordPress from putting everything in paragraph tags.

Advertisements

2 thoughts on “Home Page Sidebar”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s