Thord Daniel Hedengren - Smashing WordPress_ Beyond the Blog-Wiley (2014)

(avery) #1

CHAPTER 4 • WordPress Theme Essentials 99


The preceding template, for example, could be named mypagetemplate.php. It may be a good
idea to name Page templates pagetemplate-[something].php for semantic reasons, but that’s
entirely up to you. Just keep in mind that you shouldn’t name them page-[something].php
because page-[slug].php is a template file that WordPress might look for, and you could get a
clash there.


With that code snippet on top and then any kind of template file content you like, the Page
template will show up in the Page template box in the Write page section in the WordPress
admin. Just pick it, save, and there you go.


A common usage for Page templates is an archive page. Maybe you want to have a link to your
archives that displays all authors, categories, tags, and the 50 latest posts using template tags.
This just won’t work with a normal Page because you can’t put template tags within the post
itself through the WordPress admin (at least not without using plugins that open up the
editor), so you need a Page template. Add a simple Page template to your new theme,
designed for use with simple blog archives:


<?php
/
Template Name: Archives
/


get_header(); ?>




<?php
// Start the loop
while ( have_posts() ) : the_post();
?>


<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>







// The content
the_content();
?>

Browse by month



    // Arguments
    $args = array(
    'type' => 'monthly'
    );
Free download pdf