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.

See also:


Posted in: WordPress
TAGS:, , , , , , , , , , , , , , ,
Leave a comment

8 Comments

  1. web design dubai vistasad.com
    2 weeks ago

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

  2. Toby twresourcegroup.com
    Feb 13th, 2009

    Thanks for the easy tutorial. Works like a charm!

  3. Green Fitness greenfitnessworld.com
    Dec 22nd, 2008

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

    One more try.

  4. Green Fitness greenfitnessworld.com
    Dec 22nd, 2008

    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. Green Fitness greenfitnessworld.com
    Dec 13th, 2008

    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. Pablo DiCiacco onewalrus-ink.org
    Dec 7th, 2008

    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

    Ne radi ovo...

  8. Ray mephistos.com
    Nov 26th, 2008

    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

Have your say

Your email is never published nor shared. Required fields are marked *

*
*

Subscribe without commenting

About Vladimir

vladimir prelovac Hi! My name is Vladimir Prelovac. I am a computer engineer by profession and an adventurer by state of mind.

"I would love to change the world, I just don't have the source code yet."

Books by Vladimir

WordPress Plugin Devleopment Book WordPress Plugin Development: Beginner's Guide

Published by Packt Publishing, available online through Amazon. Click the image for more information.

Consulting Services

Professional WordPress Services, including custom developed plugins and themes

Expert on-site WordPress SEO consulting and an 'out-of-the-box thinking' approach to problems