Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
WordPress 5 Cookbook

You're reading from   WordPress 5 Cookbook Actionable solutions to common problems when building websites with WordPress

Arrow left icon
Product type Paperback
Published in Mar 2020
Publisher Packt
ISBN-13 9781838986506
Length 660 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Rakhitha Nimesh Ratnayake Rakhitha Nimesh Ratnayake
Author Profile Icon Rakhitha Nimesh Ratnayake
Rakhitha Nimesh Ratnayake
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. Setting Up WordPress and Customizing Settings 2. Customizing Theme Design and Layout FREE CHAPTER 3. Using Plugins and Widgets 4. Publishing Site Content with the Gutenberg Editor 5. Managing Users and Permissions 6. Setting up a Blogging and Editorial Workflow 7. WordPress as an Application Framework 8. Improving Usability and Interactivity 9. Building E-Commerce Sites with WooComerce 10. Troubleshooting WordPress 11. Handling Performance and Maintenance 12. Improving Site Security 13. Promoting and Monetizing the Site 14. Other Books You May Enjoy Appendix

Creating page templates from scratch

We use templates to keep the site design separate from the logic and reuse it on multiple pages without duplicating the code. WordPress page templates is a feature that allows us to create custom templates and reuse them across multiple pages of the site. These page templates are used effectively in most premium themes to provide designs for common pages that are needed for a site. The knowledge of creating templates is useful for making even minor changes to the default template.

In this recipe, we are going to create a basic page template from scratch using custom coding.

Getting ready

Open the code editor and make sure you have access to the theme files in your local or external WordPress installation.

How to do it...

We can create a page template to use products in a site. Usually, a product landing page is different to a normal page and has different headers and footers. Let's create the product page template. Follow these steps:

  1. Log in to the WordPress Dashboard as an administrator.
  2. Click the Pages menu.
  3. Click the Add New button.
  4. Go to Page Attributes section on the right hand column.
  5. Click dropdown field for Template setting to get a screen similar to following:
  1. Open the wp-content/themes/twentytwentychild folder using a file manager.
  2. Create a new template called product.php.
  3. Add the following code to the product.php file to define the template as a WordPress Page Template:
<?php /* Template Name: Product Landing Page */ ?>
  1. Save the changes.
  2. Add the following code to the product.php file to display product information:
<?php get_header(); ?>
<section id="primary" class="content-area">
<h2>Product Page</h2>
<main id="main" class="site-main">
<?php
while ( have_posts() ) :
the_post();
get_template_part( 'template-parts/content', get_post_type() );
endwhile;
?>
</main><!-- #main -->
</section><!-- #primary -->
<?php get_footer();
  1. Click the Pages menu.
  2. Click the Add New button. You will be taken to the following screen:

As you can see, the settings section now contains a new setting called Template, under Page Attributes.

  1. Add a title and content for the page and select Product Landing Page as the Template.
  2. Click the Publish button to create the page.

Now, we can view this page on the frontend. We will see that the custom page template is used instead of the default template.

How it works...

We don't have any page templates by default in our child theme. This means that the Template setting on the pages screen is not enabled until we create a template. WordPress uses the Template Name value in the comments section to identify the page templates:

<?php /* Template Name: Product Landing Page */ ?>

Once a comment with Template Name is added, WordPress will consider that file as a page template. The name of the template is the value we define after the Template Name key.

In step 7, we added the elements for the product template. Here, we used a copy of the default page.php template with minor modifications. We removed the comments section and added a new header called Product Page.

Next, we used this template while creating pages using the Template field on the post edit screen. Once the page is saved, the page template name will be stored in the wp_postmeta table with a key called _wp_page_template. The value of this key will be product.php in this case. Once the user requests this page, WordPress will look for the settings to check if the page template is enabled and load the custom template instead of the default one.

You have been reading a chapter from
WordPress 5 Cookbook
Published in: Mar 2020
Publisher: Packt
ISBN-13: 9781838986506
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime