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.


More like this:


Posted in: WordPress
TAGS:, , , , , , , , , , , , , , ,
Both comments and trackbacks are currently closed.

8 Comments

  1. Sep 1st, 2010 7:13 AM

    Thanks for sharing interesting post on custom page template in Word-press themes

  2. Feb 13th, 2009 1:47 AM

    Thanks for the easy tutorial. Works like a charm!

  3. Dec 22nd, 2008 12:31 AM

    ?php if (! is_page('forum') || is_page('google-search') ) get_sidebar(); ?

    One more try.

  4. Dec 22nd, 2008 12:30 AM

    I can't get my post to show the code I put in my footer.php. So I am trying again here it is.

    -""-

  5. Dec 13th, 2008 5:26 PM

    Awesome this is the post I had been waiting for and it works well for the forum. I am also trying to get google custom search for adsense to work so the results would display in a separate page. I took what you had given on the forum which works for me as displayed here http://www.greenfitnessworld.com/forum. However if you go to http://www.greenfitnessworld.com/google-search the side bars get shifted over for some reason. I created a new template like this for google search.

    In the footer.php I added this.

    It is probably something I am doing wrong with the OR statement. If someone could respond that would be great.

  6. Dec 7th, 2008 9:06 AM

    To Ray above me here...

    The "Headspace" plugin says it has the ability to assign different theme styles to pages. You might take a look and see if it will work for you.

    Or, you could call a separate style sheet in your custom template. Like this:
    @import "http://your-site/name-of-style.css";

    Hope that helps!

    Pablo

  7. Dejan
    Dec 1st, 2008 9:22 PM

    Ne radi ovo...

  8. Nov 26th, 2008 12:29 AM

    Like what you are doing with this and want to use it, but I don't see the option when editing (or creating) a page to use a different template. Is there something else I have to turn on to do that?

    Thanks,
    R