Создаём шаблон произвольной страницы в WordPress — 5 простых шагов

Содержание

customizing-big

Customize until you get things just the way you want them.

Today we’re going to go over creating a Custom Page Template in WordPress, something that many people eventually need to learn how to do if they ever hope to get their sites working exactly the way they want them to.

The basic creation of a Custom Page Template is not difficult, but you will need access to your server in order to do it. We’ll go over the process in five simple steps.

1. Navigate to Your Theme’s Files

As mentioned above, you will need to access the WordPress files on your server. New template files cannot be created from the Admin section of your site. Contact your web host if you aren’t sure how to do access the files on your server.

Once you are in the file system on your server, navigate to your theme’s folder (wp-content/themes/your-theme).

2. Create a New Template File

Once you are in the folder for the theme you are using, you will need to create a new template file for the Custom Page. Name the file so that it makes sense to you later. In a few months you may forget that you ever created it, and so you’ll want the name to be as descriptive as possible.

It also might be a good idea to name this file so that it ends with “-page.php” (again, to help you remember what it is). So, for example, you might name it something like newheader-page.php or fullwidth-page.php ormusic-page.php.

In any case, the file must end with the .php extension.

In my example, I’m just going to name my new file test-page.php.

name-new-file

3. Copy Default Page Template

Many people often like to start a Custom Page Template by using the information from their “normal” Page Template and then modifying that. And so we’ll cover that step here.

In the same folder where you just created your new file, you should see a file named page.php. Open that file by choosing to edit it, and then copy everything you find inside of it.

copy-default-page-template

 

4. Insert Custom Template Information & Copied Default Page Template

Next, open up your newly created Custom Page Template by choosing to edit it. (In my case, I’m opening the test-page.php file I just created.)

Paste all the material you just copied from your default Page Template (page.php) into your newly created template.

Then, AT THE VERY TOP OF THE PAGE, you will need to place some code into your file and give your template a name. In my example below, I’ve given my template the name “Test.”

You can copy this code below and substitute your own name into the place where I have the word “Test.”

 

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

 

And so just to review, at this point you should have the code above AT THE VERY TOP OF THE FILE, and then you should also have the contents of your page.php file copied into your template just below the code above.

Save your new file, and you’re ready to begin customizing.

5. Customize Default Page Template

At this point your Custom Page Template is set as a template; however, you will still need to customize it, of course.

This step will vary wildly from person to person, and so just for demonstration purposes, I’ll insert a line of text into my test template to show that it works. I’ll make this text large and red and centered. Here’s the code for it in my newly created template file.

my-customization

Remember to save your file when you’re finished customizing it.

Choose Template from Pull-Down List

So, your Custom Page Template is now built, but you will still need to choose it whenever you want to use it. You do that by going to the right-hand side of the screen whenever you’re writing a Page. You should see a section there that says, “Templates” with a pull-down menu.

choose-custom-page-template

 

The Result

As mentioned above, when choosing my new template, all Pages with that template should display the red line of text that I inserted into my template file. And they do. Here’s a look at the template in action.

the-result

And that’s it.