You are here: Home » Extending WordPress » How To Create Custom Template Pages in Thesis Theme

How To Create Custom Template Pages in Thesis Theme

by Dave Doolin on July 11, 2009 · 19 comments

(Reading time: 5 – 8 minutes)

Note: I’m happy to help registered users of the Thesis theme when I can. Just point me to your account on the DIY Themes forum (you should have the link, right), we’ll take it from there.


Thesis theme for WordPress is an excellent piece of gear. What it doesn’t handle “out of the box,” it can be easily persuaded to handle with a minimum of fuss and bother. For example, custom page templates are not explicitly supported in Thesis, but are easy to implement, and I’m going to show you how.

The procedure requires 2 simple steps:

  1. Create the custom page template file
  2. Create the custom page template function

1. Create a custom page template file

Go to your thesis theme root directory, typically wp-content/themes/thesis151. You will see files named “archives.php,” “no_sidebars.php” and “custom_template.php.” Add your file there, say “cool_page.php.”

Add a single function to this file “cool_page();” It doesn’t really matter what you call it, but mirroring the file name makes it easy for everyone, including yourself, to see what’s going on. Here’s a source code listing of what that looks like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
/**
 * Template Name: Cool Page
 *
 * A custom template framework for creating pages with 
 * wholly arbitrary layouts.  DO NOT EDIT THIS FILE. Instead, 
 * use the thesis_hook_custom_template() hook
 * to specify the contents of div#content_box. 
 * Be sure specify which page should receive which code 
 * by dividing your action using the is_page() conditional tag.
 *
 * @package Thesis
 */
 
cool_page();

NOTE: Make sure you change the name of the template on line 3 so it will correctly appear in your page template menu.

By the way, if you’re finding this article useful, send an empty email to wp-weekend.customtemplate@aweber.com to get more useful tips, tricks, lessons on WordPress, news on security updates, etc. You’ll the get the cutting edge stuff a couple of times per month. It’s spam-free, privacy protected, yadda yadda yadda. You’ll have to confirm to get in; opting out later is an easy click. Just an empty email to wp-weekend.customtemplate@aweber.com does the trick.

2. Create a custom page template function

Now you need to add some code for rendering the page. Define the cool_page() function in custom_functions.php, and populate it using code you rip out of these files:

  • lib/html/frameworks.php
  • lib/html/content_box.php
  • lib/html/templates.php
  • lib/html/hooks.php

The easiest way to figure out what code to use is to print each of these files on paper, along with custom_template.php. Then follow the control of flow using a red pencil. Figure out which functions you want on your final page, circle those in blue pencil. Once you have it figured out, type it all in by hand. Actually, I just cut-n-paste, but I’m lazy and I’ve been programming long enough to follow it in my head… (is that a good thing or a bad thing…?). If you cut and paste, watch out for the “smart quote” problem.

Here’s what my code looks like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/* MY COOL PAGE CUSTOM TEMPLATE */
// Function is called from wp-content/themes/thesis/cool_page.php
function cool_page() {
 
	get_header(apply_filters('thesis_get_header', $name));
	echo '<div id="container">' . "\n";
	echo '<div id="page">' . "\n";
	//thesis_header_area();
	//thesis_content();
	echo '	<div id="content_box" class="no_sidebars">' . "\n";
	thesis_content_column();
	echo '	</div>' . "\n";
	echo '</div>' . "\n";
	echo '</div>' . "\n";
        get_footer(apply_filters('thesis_get_footer', $name)); 
}

Some of the lines are commented out to show you where to put calls for various parts of the page.

Now, when you add a new page, you should be able to select Cool Page from your template selection drop down box on the New Page page.

NOTE: The function above produces an *extremely* stripped down page, no navigation. Don’t test it on a production blog. Here’s a screenshot:

No header, footer, sidebars, nav menu... totally custom page template!

No header, footer, sidebars, nav menu... totally custom page template!

If you’re a Thesis hacker, you should give this a try. It’s easy and fun!

Other solutions

There are other ways to do this, notably by modifying custom_functions.php and adding code to select page layout based on page ID or category, etc.

I find this clunky, and difficult to understand and maintain. Because the selection is done with numbers instead of logical entities (names), it requires looking up page IDs for every page you want rendered with the custom template, and modification each time you add a page using that template.

However, it does have the advantage of being “Thesis Portable.” That is, you only need to move what’s in the custom directory and nothing else.

Lagniappe

Just to be absolutely and thoroughly pedantic about literate programming, “thesis_get_header” from the source listing above really ought to be named “thesis_get_html_head” because:

  1. thesis_get_html_head says what it does.
  2. The word “header” is overloaded and requires context to parse meaning. “html_head” means exactly one thing, no context required. Read “Head vs. Header — A critical difference for modifying WordPress themes” for in depth explanation.
  3. It confused me initially. I knew I didn’t want a header… but without including the head element of the html, I didn’t get any of the required CSS files controlling the page display.

It’s true about literate programming: elegance and good taste matter!


I run several websites using Thesis. If you’re serious about mastering WordPress, Thesis has a lot to offer, and I recommend—without reservation—that you purchase a license for your own use. Thesis has an excellent affiliate program as well, which you should join when you purchase your license.

Similar Posts:

Share and Enjoy:
  • Facebook
  • Twitter
  • StumbleUpon
  • Digg
  • Google Bookmarks
  • email
  • del.icio.us

{ 1 trackback }

uberVU - social comments
January 22, 2010 at 11:18 pm

{ 18 comments… read them below or add one }

1 Ross July 29, 2009 at 8:10 pm

I have been looking all over and here is the gist of what I am trying to find.

I would like a template (code and where to add it) for thesis 1.5.1 so that I can add posts to my pages, other than my Homepage. I tried this one plugin and it kind of hosed my site and made the “page break” function no longer work even after removing the plugin.

So if you know of a plugin or code for a template to accomplish this could you please email me. Thank you so much!

Reply

2 Dr Wordpress! July 29, 2009 at 10:43 pm

I’m not really getting what you want to do, Ross.

One thing I have learned from over 15 years working with computers is this: if there isn’t a fairly easy straightfoward way to explain what you want to do, there probably isn’t any way to do it with your current tools. The solution is either use a different tool, or do something else, hopefully similar.

Look at the bigger picture. What, exactly, are you providing your readers?

Reply

3 Joel July 31, 2009 at 6:31 pm

Hi, I very much appreciate your alternate take on Berchman’s custom templates…you clearly have strong grasp of WordPress. I noticed in the DIY forums that you mentioned using an include in the custom_functions.php file and I’ve been contemplating a similar solution, but am not looking forward to banging my head into a steel beam for several days on end.

My goal has nothing to do with a custom template drop-down option, it’s simply to separate my code so that it’s easier to read & maintain. I imagine that by using an include within the the custom_functions.php file, one could generate additional files (ie. custom/user-header.php) which contain code & also reside within the /custom folder. Maybe something like this:


function user-header() {
if is_page('Example') {
include(TEMPLATEPATH . '/custom/user-header.php');
}}

The conditional would permit you to use this as a custom header on your ‘Example’ page…I imagine the same could be done for any page &/or any page section.

Then, in the custom/user-header.php file, you would include whatever code you want for your custom header, but you would need to be sure to add this as well:

remove_action('thesis_header','thesis_default_header')
add_action('thesis_header','user-header')

Alternatively, it looks like this may already be supported in Thesis using filters with these filter hooks: thesis_get_header & thesis_get_footer.

Sorry for going on, but I’d definitely like to get your feedback on this…one more thing, do you take freelance jobs?

Thanks in advance! …(1 for 3) I think this comment should be formatted correctly, sorry again ^_^

Reply

4 Dr Wordpress! July 31, 2009 at 6:41 pm

Yes, the formatted version is readable!

Your solution looks pretty good. My bias is to get rid of conditionals as much as possible. But I don’t know the WordPress codebase well enough to say much more. Most of my comments come from having a fair bit of programming experience. Certain ways of doing things keep coming up over and over and are more or less language independent.

I’m willing to consider freelance work. I’m very, very expensive for closed source code projects. Very. Open source or GPL projects that interest me, less expensive, possibly considerably less.

Reply

5 Joel July 31, 2009 at 7:20 pm

Thanks for the fast feedback! I’ll be working on this and will report back; although, it may be weeks from now…

Reply

6 Sherwin August 4, 2009 at 3:24 pm

Please forgive me if i don’t make sense. I would like to have most pages in my blog with a different design (not more than 60 pages with different designs on them). Do i have to use Custom Pages for each page. If Yes, will the custom_functions.php page get too long for my blog to work because i see that i have to add each custom page code to the custom_functions.php page. Please help me understand.

Any other solution would be GREATLY APPRECIATED

Reply

7 Dr Wordpress! August 4, 2009 at 3:32 pm

Sherwin, WordPress isn’t really what you want to use for that kind of project in my opinion. The strength of WordPress is that it allows you to easily create a large number of web pages with uniform appearance, not for creating a large number of web pages with all different appearances.

You may well be able to extend WordPress in this direction, but I wouldn’t use a huge custom_functions.php. What I would do is register callbacks to file handles for the appropriate page, then include it on the fly.

Really, I suspect what you are looking for is something that bridges WordPress and, say, Dreamweaver. I’m not aware of any tools out there covering that niche.

What is the information you are attempting to deliver, and to what market?

Have you tried rolling your own solution using perhaps Ruby on Rails?

Reply

8 Sherwin August 4, 2009 at 4:37 pm

Thanks for your QUICK response. What i am really trying to do is set up a website with different sales page for each item (up to 60 items). Now i would like the website to carry the same structural layout but the graphics, color schemes, header etc will only reflect the product being advertised on that particular page. I also want to include some CSS to change link and text colors as well. Its more like an affiliate site basically.

Reply

9 Dr Wordpress! August 4, 2009 at 5:05 pm

Yeah, WP doesn’t do that so well… Basically, you need to set up a structure outside WP, but in the WP directory, for handling sales and landing pages. You can add them to your sitemap.xml this way. You may be able to handle 404s using WP too, after you take them down. Not sure about that. Test it, let me know.

I have a post brewing on sales pages… or maybe it’s published. Poke around a bit. Can’t recall at the moment.

Creating Landing And Sales Pages Side-By-Side With WordPress.

Reply

10 Beat Schindler October 1, 2009 at 2:47 am

Dear Doctor, (caution: html-impaired, but unwilling to give up inches from the finish line).
My blog is WP with Thesis. What I want is basically a blank page template (for zero-distraction prod reviews/sales pages). Gr8 example at http://fitnessblackbook.com/ and http://fitnessblackbook.com/best-diet-plan/ respectively.
Created and ftp’ed “clean_page.php” to location as prescribed. Doesn’t show up in my edit page template choices. Have I missed a step or line of html code?
I have this html code for use with my hoped-for blank page:

[your content here]

It works fine if I use it in conjunction with the “no_sidebar” template, but leaves me with header and stuff. Is there a blank-page cure for one like me? Thanks a bunch in advance!
Beat
PS. Love your blog. Cool blurb to encourage visitors to sign-up for the newsletter – simple & creative!

Reply

11 Beat Schindler October 1, 2009 at 2:51 am

here’s another attempt at that html code:

[your content here]


Beat Schindler´s last blog ..Dare! My ComLuv Profile

Reply

12 Beat Schindler October 1, 2009 at 2:54 am

one more try. replacing with &
&center&&table border=”0″ cellpadding=”0″ width=”700″&
&tbody&
&tr&
&td&
[your content here]
&/td&
&/tr&
&/tbody&&/table&&/center&
Beat Schindler´s last blog ..Dare! My ComLuv Profile

Reply

13 Dr Wordpress! October 1, 2009 at 9:41 am

Make sure you have the name of the custom page template at the top of the comment section in that PHP file.
Dr Wordpress!´s last blog ..How Dangerous Are Private Label Rights To Your Blogging Credibility? My ComLuv Profile

Reply

14 Custom Spam Badge Stuff February 25, 2010 at 9:17 am

Thanks! I did it :)

now my page is very ‘professional’ :)

Reply

15 Dave Doolin February 25, 2010 at 9:39 am

No, Jonathan, you didn’t. This is spam. I looked at the source code for your linked site, and you’re not even running WordPress, much less Thesis theme.

Dave Doolin´s last blog ..World Class Copywriting: Are there Bird droppings on your catapult? My ComLuv Profile

Reply

16 Tom | Ingles online gratis October 13, 2009 at 9:19 am

Hi Dr Wordpress…

I am hoping you can help me with a problem I am having implementing my own custom page using your code above.

Basically, I have followed what you have done to create a very simple page with one column and my header and footer.

I added the code exactly as you described above to custom functions. The code looks like this:


function cool_page() {

get_header(apply_filters('thesis_get_header', $name));
remove_action('thesis_hook_after_header', 'thesis_nav_menu');
remove_action('thesis_hook_header', 'add_social_links');
echo '' . "\n";
echo '' . "\n";
thesis_header_area();
//thesis_content();
echo ' ' . "\n";
thesis_content_column();
echo ' ' . "\n";
thesis_footer_area();
echo '' . "\n";
echo '' . "\n";
//}
}

/* Now we tell Thesis to use custom template */
// I don't think these are necessary... ???
remove_action('thesis_hook_custom_template', 'thesis_custom_template_sample');
add_action('thesis_hook_custom_template', 'home_pagecustom');

My problem is that the code it generates doesn’t have a closing HTML tag or closing Body tag so the page is broken. This is how the page HTML looks just before the closing body and html tags:

[snip]

Do you have any ideas at all?!

Thank you,
Tom

[12/11/2009: The listing is correct now -- Dr W]

Reply

17 David Frosdick October 21, 2009 at 1:51 am

Thanks Doc!!

That done the trick and saved me a couple of hours coding!! I managed to remove my custom footer as well.
David Frosdick´s last blog ..Google’s New Fade In Home Page My ComLuv Profile

Reply

18 Dr Wordpress! October 21, 2009 at 7:47 am

@David, glad you liked it! Watch for more in this space in the future.
Dr Wordpress!´s last blog ..Practical WordPress Tip #17: Use the tail of your Drafts queue for “notes” My ComLuv Profile

Reply

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

CommentLuv Enabled

Previous post:

Next post: