A question I field on a regular basis is “how do I make a page template in Genesis?” It’s actually very easy to do, and I have a template I want to share for my Production theme.
Two users in a day asked how to combine videos and posts into a single blog view. Merging post types is pretty simple, you just need a custom loop. However, for people that are new to Genesis, making the page template can feel daunting. Hopefully this article will make it much less so.
Step 1: Create the File
You will need to add a new file to your child theme folder. It must be a php file, and must be in the same directory as the style.css and functions.php files. The name isn’t strictly speaking important, but if you look in the Genesis directory you will find two page template files named page_archive.php and page_blog.php. I personally recommend using this same pattern because it makes it easy to identify what your template files are at a glance and helps to remind you what the file does.
For this specific template please make the new file page_video_blog.php and upload it to the child theme directory. If you aren’t sure how to make a new php file, follow these simple steps.
- Start a basic text editor, like Notepad (alternately you can use an advanced editor like Notepad++ or Netbeans, but if you know about those you probably don’t need these instructions).
- Add your code (which we will discuss later).
- Use the “save as” menu option and be sure to select “all” file types then type in page_video_blog.php, or whatever name you will be using.
Step 2: Name Your Template
WordPress will scan every php file in the theme directory (the same level as the style.css and functions.php) for some specific code. That code makes your template work. Fortunately this is super easy. For the sake of this tutorial, put this at the top of your file.
<?php /* * Template Name: Video Blog */
The first line starts php. The next 3 lines are a php comment. This doesn’t run any actual code, but WordPress is looking for it. The “Template Name:” part is what clues WordPress in on what this file is, and “Video Blog” will be the name used to identify your template. You can see it in the Page Attributes image on the right.
Step 3: Use Actions and Filters to Change the Page Output
This is the real guts of the page template. As I said, 2 different people asked about adding Videos and Posts to a single blog template so I am showing how to build that template. This code goes after the Template Name code and comprises the rest of the file.
/** * This removes the loop. * If you want to include the static page title and content, * remove or comment out this code. */ remove_action( 'genesis_loop', 'genesis_do_loop' ); add_action( 'genesis_loop', 'child_do_video_blog_loop' ); /** * This builds the video blog loop. * The blog options in the Genesis Theme Settings are applied. * A custom field on the page with the name query_args can be used. * This will change the posts and videos pulled into the loop. * By default it will pull in all posts and videos. */ function child_do_video_blog_loop() { $include = genesis_get_option( 'blog_cat' ); $exclude = genesis_get_option( 'blog_cat_exclude' ) ? explode( ',', str_replace( ' ', '', genesis_get_option( 'blog_cat_exclude' ) ) ) : ''; $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; /** Easter Egg */ $query_args = wp_parse_args( genesis_get_custom_field( 'query_args' ), array( 'cat' => $include, 'category__not_in' => $exclude, 'showposts' => genesis_get_option( 'blog_cat_num' ), 'paged' => $paged, 'post_type' => array( 'post', 'video') ) ); genesis_custom_loop( $query_args ); } //loads the framework genesis();
You can see an example of this code output here (notice that the first post in the image is a post, and the second is a video)
The code is internally documented, so I’m not going to explain every part. I do want to focus on the last line. That is required and must be the last line.
Of course, only a limited number of people are looking to do this. Most are after some other custom functionality. If you aren’t familiar with working with actions and filters I’d recommend reading up my Genesis Explained series. The first several articles go into in depth detail on working with actions and filters, at which point you will be able to do just about anything with Genesis.
Benjamin Ficker says
Thank you so much for this! It’s a HUGE help!
Keith Davis says
Thanks for this Nick
Any chance of showing us how to set up a template that could be used as the home page of a business site.
The sort of home page that many of the Genesis child themes have.
An example might be…
Some form of full width slider followed by… 3 or 4 featured pages / posts followed by… a widgetised footer and the Genesis footer at the bottom.
Cheers Nick
Bharat Chowdary says
I like the filters part, its really creative and help to get output based on requirements. Thanks Nick.
Dailyblogshout says
I was looking this article since long time and really found this helpful and awesome.
Thanks
Mark Buskbjerg says
Great… agrees with Keith… a tutorial on a more business like page template would be nice
Mamun says
Its really Helpfull to me…. Thank you very much….