How to create a Custom Template In Genesis

This has to be one my favorite things when building a child theme with the Genesis Framework. The process is simple.

We first want to make sure we create a blank file to use as our template. Open up your favorite text editor and lets just start with a blank page. I always name my file at this step just so I don’t lose track what I am doing (if you are creating a template to overwrite one the default files make sure you name it so; for example: index.php, single.php page.php, ect.).

Next, in our blank canvas, include the template header. This is no different from creating a custom template for any other WordPress theme.
Drop the following code at the top of your file. This name will show what is in the drop down for templates.

<?php /* 
Template Name: Template Name Here
*/ ?> 

This is where things change. Normally you will begin to include the header get_header(); but, instead the template file in genesis acts much like functions.php: Using hooks and custom function we can change the output of the template file. For example, if this template was to only query a custom post type you have set, you can use the genesis_loop hook to add your own custom loop.

<?php
remove_action('genesis_loop', 'genesis_do_loop');
/**
 * Example function that replaces the default loop
 * with a custom loop querying 'PostType' CPT.
*/
add_action('genesis_loop', 'gt_custom_loop');
function gt_custom_loop() {
global $paged;

    $args = array('post_type' => 'PostType');
    // Accepts WP_Query args 
    // (http://codex.wordpress.org/Class_Reference/WP_Query)
    genesis_custom_loop( $args );

}
?>

Since this function is only present in this template it will only be called on a page using the custom template.

Finally, we end the file with a function from genesis that calls in the rest of the template.

<?php genesis(); ?>

*note – This function is only available for child themes running off genesis v1.3 for earlier versions. use require_once(PARENT_DIR. '/index.php'); . However, it is highly recommended you upgrade to the latest version.

Save the file and push it up to your theme directory. Create or find the page you were wishing to use it on and select your template from the drop down of available templates. Once you publish your page you should see your custom template in action. Simple as that.

Recommended way: custom-template.php

The positive of creating custom templates this way is the amount of code. No need pulling in any the structural divs or default functions you would want to use. It is all included within genesis();. The other advantage of this method is you future proof your template file.

I know understanding hooks can be a little complicated so, I have included a template file that is the actual structure containing the main genesis hooks in their place. You may use this template to build templates, though it is not recommended. Think of it more as a learning tool.
Non-recommended way: custom-template-alt.php

Comments

  1. says

    I was going to create some custom POST templates, and put them and single.php into the child directory, then add this to single.php:

    post;
    if ( in_category(’10′) ) {include(CHILD_DIR.’/singlePostTemplate10.php’);}
    elseif ( in_category(’20′) ) {include(CHILD_DIR.’/singlePostTemplate20.php’);}
    elseif ( in_category(’30′) ) {include(CHILD_DIR.’/singlePostTemplate30.php’);}
    else {include(CHILD_DIR.’/singlePostTemplate0.php’);
    }
    ?>

    Should I use your custom_loop function idea instead? I don’t understand what it does.

    • Avatar of Christopher says

      Dave,
      genesis_custom_loop() is meant to be executed when a custom query is needed. It accepts arguments in query_posts style format to modify the custom WP_Query object (for parameters you can pass you can look at the query_posts() codex page).

      Depending on what your templates will do you may or may not want to use it.

      For example, on this site I use the genesis custom loop in a custom template for the Snippets page to only query snippets in a custom post type.

      <?php /* Template Name: Snippet Index */
      
      remove_action('genesis_loop', 'genesis_do_loop');
      add_action('genesis_loop', 'custom_loop');
      function custom_loop() {
      	echo '<h1>'; the_title(); echo '</h1>';   	
         	echo '<h3>A Collection of Genesis code snippets</h3><hr />';   	
          global $paged;
          $args = array('post_type' => 'Snippet');
          genesis_custom_loop( $args );
      }
      remove_action('genesis_before_post_content', 'genesis_post_info');
      	
      genesis(); ?>
      

      You can then still use your category logic to call which template to use.

      Hope this helps!

      -Christoper

  2. Wes says

    Great tutorial. Personally I find all these hooks and custom functions confusing.

    Is there a way to specify a genesis layout to use within the custom template? Instead of the default or manually selecting one, I’d like to do this based on the template selected unless a layout is selected.

    So in case that was confusing… 1) Using custom template, 2) Want to select a genesis layout for this custom template 3) If a layout other than default is selected, then it overrides the custom template layout.

    • Avatar of Christopher says

      At first getting use to the hooks and functions specific to genesis is a little confusing but once you get the hang of it you will really appreciate it, Least I do. I Think of Genesis almost as a bicycle, and once you learn to ride it you can get to where you want quicker.

      Anyways to answer your question.. If I understand it correct you want to set a default layout like content/sidebar/sidebar to whatever the set layout is, but still allow them to change?

      Genesis allows you to force a layout of a page or of the whole site see Forcing Home Layout for an example. The example in the link I gave is for setting the homepage layout and can place that in your child theme’s functions.php. But to do it for a specific template just take out the conditional and drop it into your template’s file. Now to do what you wanna do… This code may be exactly what you are looking for.. I hope.

       
      add_filter( 'genesis_pre_get_option_site_layout', 'gt_set_template_layout_default' );
      function gt_set_template_layout_default( $opt ) {
      $layout = genesis_get_custom_field( '_genesis_layout' ); // gets the inpost layout value of the selected layout.
          if ( !$layout ) // when the layout is set to default holds no value, so if $layout isn't true do your set layout.
          $opt = 'full-width-content'; // view the link above I gave for the homepage to see the other values available here.
          return $opt;
      }
      

      That snippet above will set the page template to full width unless otherwise told to do something different by the inpost settings.

  3. says

    I’m with Wes in that I find the hooks and functions confusing and counter intuitive. I used to modifying a normal WordPress theme and editing a single.php, keeping the header and footer, and doing what I want with my own loop in the middle, or just pasting in some HTML. You don’t seem to be able to do that with Genesis.

    • Avatar of Christopher says

      I would say that hooks aren’t for everyone. Though about being counter intuitive, it may seem that way at first but once you begin getting use to the names of hooks and built in function you can quite easily and rapidly produce themes. Then again it all is a matter of preference.

      Rob, did you see the link to an alternate template?

      custom-template-alt.php

      This is a little easier and can add whatever code you like without dealing with hooks.

      Maybe what I’ll do is break this template either further down to see post-titles, post content and the sidebars. Not exactly to use ( though you would be able to if you like), but more as an educational tool.

      • says

        Sorry, Christopher, I hadn’t seen that template and, yes, it’s a little easier to get my head around as it’s more similar to the custom templates you make for other WP themes. Thank you for all your help. This site is as useful as the Studiopress site for me.

  4. Craig says

    I understand the custom-template-alt.php, that is very helpful. Now, if I want to have the posts not show the author or the date (only in this new template) how do I modify for that?
    Great stuff, by the way

  5. Tess says

    Thanks for this! It really helped me out. I could not figure out how to make a new template page in a child theme – needed it to add the dynamic content gallery to a page – worked great!

  6. says

    Thanks for the link to the custom-template-alt.php page. I used it for a custom category page where we wanted to add text to the top of the page. Worked a treat – example for anyone who is trying to do something similar: freeu.com/testimonials/

  7. says

    Hi! So, I’m interested in having a custom page in a Genesis child theme that has none of the main template elements: no header, footer, sidebar, etc. I just want it to have a white background that I can put a video and some text on. I want to use these pages with lightbox. I’m new to Genesis and trying to wade through what seems like should be a simple solution. Any thoughts? Thanks!

  8. says

    Very useful tutorial. I am using buddypress1.5 and Lifestyle theme. I am displaying the posts, related to specific users, below the profile fields in Buddypress user profile page. Here I am using a custom genesis loop for displaying the posts. Every thing is working fine except the side bar displays from middle of the page i.e starting from where the genesis posts are being displayed. So can i override this function “genesis()” cause this is the one that pulls the sidebar. Also the footer got collapsed. Here is the link http://sustainablenorthflorida.org/beta/members/trish/profile/. Please help me out . Thanks in advance.

  9. Minh Tran says

    Thanks for your tutorials. I would like to know how to create a custom loop that queries the titles and post info without content or excerpt (for home.php and archieve.php).

    Thank you

  10. says

    Hello Christopher
    I tried to open the zip files but I just get an error message saying this file is empty or contains one part of a multi part zip file. I’ve tried using Windows and Linux computers to download the files but still get the same error.

    Are the zip files still available or am I doing something wrong?
    PS. thanks for the great tutorials.

  11. Lorin says

    I don’t understand. I try to download you php files but the link are for zip files that say they have nothing in them. I download them anyway, try to open them, and there’s nothing inside.

    I even tried changing the file extension to just .php. I open with Dreamweave/TextEdit and they’re blank.
    What am I missing?

Trackbacks

Leave a Reply

Your email address will not be published. Required fields are marked *

You may 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>