Lesson 8: Page Templates Wordpress


The only template page.php may not suffice.

We have just seen what was in the page.php template. It is rather basic but plays its role.

One of the features of Pages, and I had already talked about it, is to create templates page. It is simply a file where you put the PHP code you want. This code will be displayed through a page, as long as you tell it (this page) using this template.

This is done so in 3 steps:

Create a template Page
Create a page in the WordPress admin
Tell it to use the template created earlier

Creating a template Page
A template page is simply a particular PHP file. You can give it any name you like, as long as it is a reserved names by WP.

For example create a file that will identify some site statistics:

number of Posts
number of Pages
number of comments

a list of pages (that is gift that)
Page.php's duplicate and rename it stats.php (or hasta la vista.php, the choice is free and not penalizing for later). The first thing to do is to convert that file into a template page. It could not be simpler. We must add the following piece of code at the beginning of the file:

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

This defines not only stats.php file as a template page but the template name itself.


Here is the entire code of stats.php file:

<?php
 /* 
Template Name: Statistiques 
*/
 ?> 
<?php get_header(); ?> 
<div class="main page"> 
<?php if (have_posts()) : ?> 
<?php while (have_posts()) : the_post(); ?>
 <div class="post">
 <h1 class="post-title">
<?php the_title(); ?>
</h1> 
<div class="post-content">
 <p>Number of Posts : <strong><?php echo wp_count_posts()->publish; ?>
</strong></p> 
<p>Number of Pages : <strong><?php echo wp_count_posts('page')->publish; ?>
</strong></p> 
<p>Number of comments shared : <strong><?php echo wp_count_comments()->approved; ?></strong></p>
 </div>
 </div>
 <?php endwhile; ?> 
<?php endif; ?>
 </div>
 <?php get_sidebar(); ?> 
<?php get_footer(); ?>

You'll notice that the function the_content () has disappeared in favor of the 3 paragraphs. It is nevertheless possible to keep:

<div class="post-content"> 
<?php the_content(); ?> <p>Nombre de Posts : <strong><?php echo wp_count_posts()->publish; ?></strong></p>
 <p>Nombre de Pages : <strong><?php echo wp_count_posts('page')->publish; ?>
</strong></p> 
<p>Nombre de commentaires publiés : <strong><?php echo wp_count_comments()->approved; ?></strong></p> 
</div>

In this case, the content that you return in the WYSIWYG admin WP will be displayed before the 3 paragraphs.

Using a template Page

We created the file. Now we have to show it. For this, you must first create a page in the WordPress admin. Name it as you like (mine is called "Site Statistics"). Fill in your template content if the poster.

Then move your gaze to the right of the screen and you will see a block "Page Attributes".

Here summarized the specificities of Pages which we had already spoken, namely:

Hierarchy of Pages ("Parent")
Template Page
Order of Pages

One that concerns us now is the second. Click the drop down menu and select "Statistics".

"Default Template" is page.php.
"Statistics" is stats.php.

That's it! We just make the connection between our template stats.php and Page "Site Stats".


Previous
Next Post »