How to use custom page template in WordPress themes

A reader recently suggested I should expand my forum page to show without the sidebar, so it's more easier to follow.

Since the forum is actually a page where the forums is rendered by a plugin, it is possible to select a template for the page (this is a feature of WordPress, found on edit page screen). Page template allows you to specify a custom look for your page, for example without restrictions in width like I need.

Page template I created looks like this:

<?php
/*
Template Name: Forum
*/

get_header();

if ( have_posts() ) while ( have_posts() ) { the_post();  the_content(); }

get_footer();
?>

You first specify the template name, in this case I used Forum. I then call he header of the theme (get_header) to render the upper menu with navigation.

After is the WordPress 'loop' which will print out my page.

At the end I call the footer code (get_footer). Since my theme actually calls the sidebar in the footer I had to add this code to the footer.php:

<?php if (! is_page('forum') ) get_sidebar(); ?>

The code says do not display the sidebar if the page is forum.

The last thing is to select the forum template for the page and the magic happens - my WordPress forum is now full width.


About this entry