Skip to content

How to create custom WordPress Page templates – for example a page showing subpages

Create a php file with some unique name (like snarfer.php) and put it in your WordPress theme directory.

This is the way you create a WordPress Page templates:

<?php
/*
Template Name: Snarfer
*/

This is an example of a Worspress Page template which displays a Page including subpages:

<?php
/*
Template Name: Page Including Pages
*/

get_header();
include (TEMPLATEPATH . "/sidebar.php");
?>

<div id="content">

  <?php
  if (have_posts()) : while (have_posts()) : the_post(); ?>
  <!--
  <h1>
    <?php the_title();?>
  </h1>
  <div>
        <?php the_content();?>
  </div>-->
  <?php $page_id = get_the_ID();
  endwhile;
  endif;

query_posts(array('showposts' => 99, 'post_parent' => $page_id, 'post_type' => 'page', 'orderby' => 'menu_order', 'order' => 'ASC'));

$post_counter = 0;
while (have_posts()) : the_post();
$feat = get_post_meta($post->ID, 'featured', true); 

if (($feat != '1')&&($feat != '2' )) {
$post_counter = $post_counter + 1;        
?>
  <div id="<?php echo "landing_posts" . $post_counter; ?>">
    <div>
      <?php
            if ( has_post_thumbnail() ) { ?>
      <?php
                        $imgsrcparam = array(
                        'alt'    => trim(strip_tags( $post->post_excerpt )),
                        'title'    => trim(strip_tags( $post->post_title )),
                        );
                        $thumbID = get_the_post_thumbnail( $post->ID, 'one', $imgsrcparam ); ?>
      <div><a href="<?php the_permalink() ?>"><?php echo "$thumbID"; ?></a></div>
      <?php } else {?>
      <div><a href="<?php the_permalink() ?>"><img src="<?php bloginfo('template_url'); ?>/images/default-thumbnail.jpg" alt="<?php the_title(); ?>" /></a></div>
      <?php } ?>
    </div>
    <!-- .sepia -->
    <h2><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
      <?php the_title(); ?>
      </a></h2>
    <?php the_excerpt(); ?>
    <?php $datemeta = get_post_meta($post->ID, 'date_hidden', true);  if ($datemeta != 1) {?>
    <div>
      <?php the_time('D, d M, Y | G:i'); ?>
    </div>
    <?php } ?>
  </div>
  <?php
}

endwhile;

wp_reset_query();  // Restore global post data
edit_post_link('Edit this entry.', '<p>', '</p>');
?>
</div>
<div style="clear: both;"></div>
<?php include (TEMPLATEPATH . "/bottom.php"); ?>
<?php get_footer(); ?>

Conditional tags for a Page templates are:
is_page_template()
is_page_template( ‘the_name_of_yout_page_template.php’ )
More info for conditional tags – http://codex.wordpress.org/Conditional_Tags

Before you see the newly created WordPress Page template you may need to go to your theme manager and reactivate your current theme, otherwise you may not see the new Page template in the drop-down inside the administration of your pages.

For more info and other page template examples visit: http://codex.wordpress.org/Pages

Published inCodingWordpress

Be First to Comment

Leave a Reply

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

*