WP Page Templates

Because I had an absolute brain fart and temporarily forgot about how WP handles custom page templates, I decided to document my recent recollections to help New Users or Old Users that just forget, in the future! I believe the dafault WP theme (Kubrick) includes 3 user accessible page templates by default:

  1. The Default (Blank static page)
  2. Archives (Displays post archive format depending on weekly, monthly, annually, etc)
  3. Comments (Displays format for single post page with comments)

You are able to create a “custom” page template anytime you want to (excluding some WP hosted services that don’t allow it). One example of a use for a custom page template would be my “Reviews” page. I wanted to only display posts from the “Reviews” category on that page. I more or less just copied the “page.php” template included in any theme, added a Template header, and modified the WP Loop to only show the desired posts.

Template Header:

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

Note: This must be in the top of a page before the “Page Template” dropdown will appear under the “Quick Edit” or “Edit” options for a page in the WP Site Admin area.


Modified WP Loop:

<?php
query_posts(’cat=8′);
while (have_posts()) : the_post();
the_content();
endwhile;
?>

<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>

Note: I am currently having an issue with a post repeating itself with this loop, will update once it is fixed. Suggestions welcomed!

———————————–Update————————————————————-

Ok, I finally figured out the fix to my problems with duplicate posts on the “Reviews” page. Here is the correct modified WP loop for displaying posts of a specific category:

 

<?php query_posts(’cat=8′); ?>

<?php if (have_posts()) : ?>

<?php while (have_posts()) : the_post(); ?>

——————————————————————————————————–

 

 


Once this is done, there’s one more step:

There are 3 ways that i know of to do this:

  1. Quick Edit
    • Navigate to the “Pages” section within your WP Site Admin area
    • Hover over a page title and select “Quick Edit”
    • Select the Appropriate template form the “Page Template” dropdown menu
    • Apply your changes
  2. Edit Page
    • Navigate to the “Pages” section within your WP Site Admin area
    • Hover over a page title and select “Edit”
    • Select the Appropriate template form the “Page Template” dropdown menu on the right (under “Parent” dropdown)
    • Apply your changes
  3. Manually via DB
    • Add a new record to the “wp_postmeta” table
      • meta_id = next sequential number
      • post_id = id of the page you want to apply this to
      • meta_key = “_wp_page_template” (actual tag minus quotes)
      • meta_value = name of Page Template file (i.e. reviews.php)

I do not at all recommend method # 3 for many reasons, please only use this if something breaks…..really bad!

Share

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

No comments yet.

Leave a comment

(required)

(required)