How to add your Page Templates to WordPress? WordPress Codex Pages

Guys, hello everyone. Alexander Borisov is with you. Since there is now a fierce struggle for a place in the sun in the world of Internet business, I devote a lot of time to development strategies, strengthening defensive positions and something else secret, I don’t write so often.

No problem, now I’ll implement all my plans, and then I’ll write a lot, and we’ll talk to you about what I said above. I think that many already feel what is happening online, so now only the strongest will survive. Oh, I don’t envy newcomers... Ok, in short don't miss my articles about this theme.

In today’s article I would like to tell you about one feature that will not be something new for many of you, but will be very useful for beginners. Well, to be honest, I’m far from a newbie myself, but I learned about this thing not long ago, when, as they say, it came to a head.

Basically, today I will tell you how to make a separate template for your blog pages or posts (articles). If anyone doesn't understand what I mean, I'll show you specific example. Right now you are reading this article, but didn’t you notice that at the beginning of the article you didn’t see two banners of size 468x60?

We didn’t see the 468x60 banner, which is located at the end of the article. Didn't see the cackle comment widget, Google advertising Adsense... Did you notice? And look at other articles on my blog. It's all there. .

How did I manage to do this? As you know, the file single.php, which is located in the folder with the wp-contetn/themes theme, is responsible for the output of all articles on our blog. So here's to writing new article and remove something from it that you don’t need or, on the contrary, add, you just need to make sure that this particular article is output using another file.

Not the standard single.php, but for example single-2436.php or post.php, which are located in the same folder with the theme of your blog. These files should contain absolutely the same thing as the single.php file, only of course you remove or add something there as you need.

Well, for example, in my single.php file at the beginning there are codes that display two 468x60 banners in all blog posts. To make sure that new article these banners were not there, I just need to remove these codes in the new file single-2436.php or post.php (depending on which of the two methods I suggested you choose next).

I think you understand me. Actually, your question is, why create other page and post templates in WordPress? Well, if you think carefully, you will understand why. I will now give you just one example.

Well, in principle, I agree to place their banner, but how can I do this if everything is the same in all my blog entries? Same template for all articles. Do you understand what I mean? I think that I even already interesting idea threw it into your motsk. =)

The solution turned out to be simple. You need to create a separate post template and that’s it. Let me now tell you how these templates are created in two ways, and you can choose the one that suits you. I’ll say right away that I chose the second option. Creating a template without a plugin. I don't like them.

No. 1 - single.php template using the Custom Post Template plugin

This is what it will look like:

Ready! Now, when you write a new article and you need it to be displayed with this particular template, you simply select the template that you created in the article itself on the right:

Need another wordpress post template, no problem. Create another file, for example post2.php, also change the code there (insert the code at the beginning, only with post2) as you need, and when publishing an article, select post2.php in the Post Template. Elementary! By editing the template you can change anything! Get busy...)))

No. 2 - single.php template with a small bunch in functions.php

Step #1— Place this code at the very beginning of the functions.php file:

/*Defining the post ID*/ function single($template) ( global $wp_query, $post; if(file_exists(TEMPLATEPATH . "/single-" . $post->ID . ".php")) ( return TEMPLATEPATH . " /single-" . $post->ID . ".php"; ) if(file_exists(TEMPLATEPATH . "/single.php")) ( return TEMPLATEPATH . "/single.php"; ) return $template; ) add_filter( "single_template", "single");

Step #2— When writing an article for a blog (even before publication), we calculate its ID. It's not difficult, there is its number in the browser line:

Mine is 7525.

Step #3— We create a copy of the single.php file, only we call it single-7525.php

Step #4- Again, in this new file we add or remove something as we need.

Step #5— We publish an article on the blog.

After WordPress publications, due to the bundle that we added to functions.php, it will understand that this article should be displayed not in single.php, but in single-7525.php. I think it's clear.

What is better plugin or bundle?

Now not a big conclusion. What better plugin or a bunch of functions? It all depends on your needs. If you need many templates wordpress posts, then a plugin is probably better, you yourself understand that if you use a bunch, you will have to create for each record new file in the folder with the theme, but it’s funny when there are 100 or 200 of them.

If there are 2-3-5 templates, then you can fart in functions. Well, that's basically all. I don't think it's anything complicated. It seems like a small thing, but it’s actually a very cool feature.

How to make a WordPress page template

Well, everything is simple here. Once upon a time I briefly talked about this in an article about it for the blog. The scheme is simple, you don’t even need any farts or plugins.

The file page.php is responsible for displaying blog pages, which is also located in the folder with the theme of your blog.

Step #1— make a copy of this file in the theme folder

Step #2- rename this file for example to puk.php

Step #3— We change everything in this file as we need.

Step #4— Add the following code to the beginning of the puk.php file:

Step #5— We publish new page to the blog, just select the template we need on the right:

Ready! Well, that seems to be all for today. Maybe someone has some questions? In theory, he explained everything in a way that even the samovar himself could understand. Finally, an interesting announcement. Remember in the KSBT 3.0 course I said that a good link profile is the key to successful promotion? So here's some advice:

Best regards, Alexander Borisov

In this article I will talk about ways to create templates for permanent pages WordPress. Each method has its pros and cons. But before we begin, a little about what pages are and how they differ from posts.

In WordPress you can create pages (pages) and posts (posts). They differ in that the entries: end up in the feed on home page; Categories are indicated for entries; posts cannot be tree-like, and pages: are used for content such as “About me”, “Contacts”, “Site map”; do not have categories, but have a tree structure. Entries are usually intended for chronological information (based on the time they were added), and pages are for a tree structure that is not dependent on time. For example, this article is published as an “entry” in the “Code” section, and links in the header menu lead to the pages: Functions.

Pages are similar to records - they are located in the same database table and their data is almost the same: title, text, additional fields etc. Both are records, but different types: Pages are tree-like and organized by creating parent and child pages, and posts are organized by categories and tags. In WordPress you can create additional post types, tree or not.

Creating Pages in WordPress

Often you need to create a separate page template so that the information displayed differs from other pages. By creating a page template in WordPress, you can completely change the page: remove the sidebar, footer, header, or change the page beyond recognition. For example, on this site, the page on which WordPress file codes are displayed is changed in this way.

Method 1: page template using a file with a custom name and connecting it in the admin panel (classic method)

This is the most common way to create a page template in WordPress. To do this, you need to create a .php file, for example, tpl_my-page.php in the theme folder and at the very beginning of the file write a note that the created file is a template for pages:

Now, when creating a page in the admin panel, in the “Page Properties” block, we can select a “template”:

From WordPress 4.7. Such page templates can be created for any type of post, not just page. To do this, supplement the comments with the line: Template Post Type: post, page, where post, page are the names of the post types to which the template belongs.

/* Template Name: My page template Template Post Type: post, page, product */

Advantages:

    Having created one template, we can conveniently use it for different pages. For example, you can create a template without a sidebar and use it on different pages.

  • Only records with the specified template can be retrieved. For example, you can display all pages with the “Services” template (servises.php file). Sometimes it's convenient. The name of the template file is stored in the _wp_page_template metafield, so to display pages with the specified template you need to create a query using the metafield (see WP_Query).

Flaws:

After creating the template file in the theme folder, you need to go to the admin panel and install the template for the page. This is not always convenient during development. Therefore, if you intend to use the template for only one page, use the second method.

How it works:

When you go to the admin panel to edit a tree post page, WordPress scans all template files for the line:

Template Name: ***

The line can be located anywhere and any way in the file.

All files with similar strings are collected and displayed in the template selection in the "Page Attributes" block.

When publishing a page, the custom field _wp_page_template is filled with the name of the template file or default if no template is specified:

Wp_page_template = default
_wp_page_template = tpl_my-page.php

Next, when the user visits the page, WordPress will check the _wp_page_template meta field, if the template is installed, then the template file is used. Otherwise, the search for the page template continues through the hierarchy.

Method 2: page template via a file with a specific name (hierarchy of template files)

When a page is created, a label (slug, alternative name). It is used in the page URL. And it can be changed:


To create a template this way, you need to find out the page slug and create a file in the theme folder. Let’s say our slug, as in the picture, is equal to contacts , then we’ll create a page-contacts.php file in the theme. and fill it out the required code(you can copy the contents from the page.php template file and edit it to suit your needs). That's it, now when we visit the page we should see new template. Similarly, you can take the ID (let it be 12) of the page and create a file page-12.php.

Advantages:

There is no need to go to the admin panel and install the template file. The template starts working immediately after the file is created. Convenient for development.

Flaws:

The template is created for only one, specific page. Depends on the slug of the page; if it changes, the template will not work. If you use ID, then the dependence on the slug disappears, but it becomes unclear in the theme file which page the template belongs to (if there are several templates with ID).

Almost useless when writing templates, much less plugins. It can be used when you edit your website, in which the slug or page ID is known in advance.

How it works:

WordPeress selects which file to use in the following order (the files must be in the theme root):

  • (any_name).php (when using a page template)
  • page-(post_label).php
  • page-(post_ID).php
  • page.php
  • singular.php
  • index.php

Method 3: page template through the "template_include" filter (coding)

This is an advanced method, it is more complex, but along with complexity it opens up ample opportunities. Using this method, you can set a template for any page, post, category, any publication on the site, or even a group of any publications. See examples with descriptions:

// the filter passes the $template variable - the path to the template file. // By changing this path we change the template file. add_filter("template_include", "my_template"); function my_template($template) ( # similar to the second method // if this is a page with the portfolio slug, use the template file page-portfolio.php // use the conditional tag is_page() if(is_page("portfolio"))( if ($new_template = locate_template(array("page-portfolio.php"))) return $new_template ; ) # template for category group // this example will use the file from the theme folder tpl_special-cats.php, // as a template for categories with ID 9 , title "Uncategorized" and slug "php" if(is_category(array(9, "Uncategorized", "php")))( return get_stylesheet_directory() . "/tpl_special-cats.php"; ) # template for entry by ID // the template file is located in the plugin folder /my-plugin/site-template.php global $post; if($post->ID == 12)( return wp_normalize_path(WP_PLUGIN_DIR) . "/my-plugin/site- template.php"; ) # template for pages of a custom type "book" // it is assumed that the template file book-tpl.php is in the theme folder global $post; if($post->post_type == "book")( return get_stylesheet_directory() . "/book-tpl.php";

) return $template; ) This code needs to be placed in the theme’s functions.php file or in a plugin, or connected in some other way. As can be seen from the example, during the template_include filter they are already working conditional tags

Advantages:

    , global variables are set: $wp_query, $post, etc.

  • You can set a template for any page or group of pages. Almost complete carte blanche in actions.

Flaws:

You can create a template when writing a plugin.

The need to write code and connect it separately (for example, in the theme’s functions.php). This tutorial will show you how to create a new page template for WordPress templates Cherry.

    Framework

    Login to your WordPress admin panel. Open menu Pages -> Add New (Pages -> Add New)

    and create a new page with a new page template. Enter a title for the page and save your changes. In the right column in the panel, you can select one of the page templates used in your theme. You need to add a new page template to this list. All these templates are located in a folder on your server. But you need to add a new page template to the folder.

    wp-content/themes/theme##### Create a new .php file in any editor like Dreamweaver or Notepad++ and name it something like: new.php

    . Add the following code to this file: Enter a title for the page and save your changes. In the right column in the panel, you can select one of the page templates used in your theme. You need to add a new page template to this list. All these templates are located in a folder on your server. But you need to add a new page template to the folder Save the file and upload it to the folder

    Now go back to the page you created in admin WordPress panels. In the panel Page Attributes You can choose a new page template called “Custom”.

    Open the new.php file again and start adding your content. The file can contain whatever you want, including immutable content or page layout elements that will display page content.

    You can use code elements of other page templates from the folder wp-content/themes/CherryFramework to create a specific page.

    Let's first add a header and footer to the new page template:

    If you save your changes, you will be able to see them on the page:

    Now you need to select the type of content you want to use on this page. Let's say you don't have a separate page template for custom “Our Team” posts. And you like the design of the Testimonials page template and would like to use this design for “Our Team” posts. To do this in the menu Design -> Editor (Appearance -> Editor) you can easily find the file that contains the code for the Testimonials page template. His name page-testi.php.

    Open this file for editing. Copy just the content code since you have already added the header and footer (below is an example of this code, your code may look different):

    " data-motopress-wrapper-file="page-testi.php" data-motopress-wrapper-type="content">
    " data-motopress-type="static" data-motopress-static-file="static/static-title.php">
    " id="content" data-motopress-type="loop" data-motopress-loop-file="loop/loop-testi.php">
    sidebar" id="sidebar" data-motopress-type="static-sidebar" data-motopress-sidebar-file="sidebar.php">

    Paste the copied code into the new.php file. Replace Template Name: Custom on Template Name: Team

    And replace page-testi.php on Create a new .php file in any editor like Dreamweaver or Notepad++ and name it something like: in this code.

    This code also contains a link to the file loop/loop-testi.php. This is another file that you need to copy and edit. Copy the file loop-testi.php from folder wp-content/themes/CherryFramework/loop and paste it into the folder wp-content/themes/theme#####/loop(if your theme folder does not have a folder loop, you should create it). Rename this file for example to loop-new.php and open it for editing. Replace the word “testi” on word "team" in all lines of the file and save the changes. (You can use the keyboard shortcut Control (Command) + F to replace words.)

    When the file loop-new.php created and edited, open the file again Create a new .php file in any editor like Dreamweaver or Notepad++ and name it something like: and replace loop/loop-testi.php on loop/loop-new.php in his code. As a result, your code will look like this:

    " data-motopress-wrapper-file="new.php" data-motopress-wrapper-type="content">
    " data-motopress-type="static" data-motopress-static-file="static/static-title.php">
    " id="content" data-motopress-type="loop" data-motopress-loop-file="loop/loop-new.php">
    sidebar" id="sidebar" data-motopress-type="static-sidebar" data-motopress-sidebar-file="sidebar.php">

    Save your changes and test your new page. It should now contain custom “Our Team” posts with the Testimonials page design.

Creating a WordPress Page Template

Often, when using WordPress, you need to create a custom template for the page. You may have noticed that many WordPress sites have different layouts for different pages. A custom page allows you to create different looks for regular pages in WordPress. If you're new to WP and have never done this before, fear not - creating a custom page template for your site is easier than you think.

Most WP themes contain page file (page.php), which contains the default page template used on each page if no specific type is specified. If you need a page with a different layout or format than the default page template, you need to create your own template type. When developing WP templates, basic knowledge of HTML, CSS and PHP is required. However, you can use ready-made WordPress themes - www.inbenefit.com if you do not have the necessary skills.

Creating a Page Template

To create your own template, you will need a text editor. For Windows, you can use a regular notepad. Otherwise, you'll need some simple editing system like FileZilla or Dreamweaver. In any case, follow these steps:

If you go to the page in a browser, you will see an empty space. This is because there is no code in the file you created to tell WordPress how to display the content. Now your HTML, PHP and CSS skills come into play.

Preparing the template

In fact, you can add any HMTL form, PHP script, or template tag to the custompage01.php file that you want to use. It might be helpful to read a little about what you can do at the coding level before using this pattern. You can also include website scripts for various purposes.

One easy way to get started with templates is to copy the basic information from the page.php file into the theme you're using. This will give you a starting point and can be useful when you first learn to program. Follow these steps:


In this case, you don't need a lot of programming skills to configure the basic functionality for your purposes. However, decide if you want to create a custom layout for your page. For example, the page will not display any content you enter into WordPress without entering a class for areas.

Does this work for WordPress posts?

Unfortunately, this only works for static pages in the control system. You will not be able to change attributes this way on screen after editing.

Will this affect anything else on the WordPress site?

The custom page is isolated from the rest of your content. Even if you code the template incorrectly, it will not affect anything on the website. If you have a problem on a custom page, just change the template to the default and everything will go back to how it was.

Is there an alternative to create layouts without coding?

You can create your own layout without going through the steps of creating a unique template. You can use a plugin like Page Builder from SiteOrigin, which gives you the ability to customize a unique layout when using widgets within the content itself. For those who do not have programming skills, this may be a better alternative.

If you prefer to use Page Builder, you can easily deactivate sidebars and footers while maintaining the site title by choosing a different page layout.

Custom pages can have a variety of purposes, from advertising a product or service to creating a landing page ideal for different campaigns.

Not long ago a manual was published about. It was of an introduction nature at the initial stage, but also included practical examples. I would like to continue this direction, just pay attention to the pages. The material on this topic is multifunctional, that is, very large. Therefore, it will be difficult to open all the features of a page template in one article. But we will at least try to touch on indirect details and, of course, the main parameters.

Page Hierarchy

Hierarchy means a sequence from the highest level to the very bottom, or vice versa. In our case, this is the same order of authority of the template files for the page. When a user lands on one of your pages, WordPress displays its content based on the page creation option (more details below).

Custom template– is indicated by a conditional comment at the top inside a file with an arbitrary name.

page-slug.php– a template with the name of the page (shortcut). If a custom template was not specified, then WordPress uses this type as the main one.

page-id.php– unique page identifier.

page.php– a standard file responsible for the content of pages.

index.php– index file. If WordPress does not find any of the templates listed above, then by default it will be responsible for the pages.

Attention. There is also a page template in WordPress called paged.php, however, it is not used to display single post pages, rather to display a list of pages in post archives.

Create a custom template for mass use

There are several options for creating a template with their pros and cons, almost insignificant. This option, which we will now consider, is the most common. The first step is to create a php file or duplicate the standard page.php. Then save it under a different name, for example, templates_my.php. Then add a comment at the top of the file:

The next step is to upload the file to your theme, go to the admin panel in the “Pages-Add New” tab or you can open an existing one. In the page attributes, select which template will be used on this page.

If suddenly you don’t have the “Page Attributes” panel, then just check the box in the screen settings in the upper right corner to display it.

Mass use means that this type of template can be applied to any pages of the site. As opposed to specialized, which was created with a specific page identifier or label.

Advice. A template created in this way can be located in a subfolder of the current theme. Thus, without taking up extra space and creating a compact look.

Important. Do not use the prefix page- as the name of the template page. Because WordPress will interpret the file as a specialized one, which clearly only applies to one page.

Custom Page Templates

The second method is intended only for certain pages without exception. Let's give an example using the old scheme. Let's say you have a page called "Portfolio", by default its label translates into the English word "portfolio". This can be seen in the “Page-Properties” settings. Now we simply duplicate the page.php file and rename it to page-portfolio.php.

The ID is created in exactly the same way, only the page ID is substituted for the label. It is located in the address bar, the “Pages-Edit” section looks like this post=9 . In the same way, the standard file is duplicated and renamed to page-9.php.

Important. A template of the same type that was created only for a specific page cannot be located in theme subfolders. Exactly the same as in child themes.

Practical example of creating a page template

Now, as an example, let’s create a template based on the first option and change the loop in it by adding the output of categories with records. Open any editor, add the code below, save it under the name template_my.php, and upload it to the server.

    Category-1

    have_posts()) : $the_query -> the_post(); ?>
  • ">

    Category-2

    have_posts()) : $the_query -> the_post(); ?>
  • ">

    Category-3

    have_posts()) : $the_query -> the_post(); ?>
  • ">

Now you need to write styles in the style.css file

My_main ( box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1); margin: 20px auto; padding: 15px; background: #fff; width:1000px; ) .page_cat ( margin: 0 9px 20px; padding: 15px; vertical-align: top; width: 28%; .page_cat h2 ( color: #676767; font: bold 18px arial; margin-bottom: 20px; ) .page_cat li a ( text-decoration : none; color: #2879af; display: block; ) .page_cat li ( margin-bottom: 6px; padding: 3px 3px 3px 10px; display: block; ) .page_cat li a:hover ( text-decoration: underline; ) . page_cat li:nth-child(2n) ( background: #f7f7f7; )

There is only one thing left to do: in the admin panel, in the “Pages-Edit” tab, specify our previously created template “example template” in the page attributes.

Result.

Conditional page tags

In the final paragraph, we will consider several conditional tags. They are written in the standard page.php file.

Displaying elements on specific pages

/images/img.png"/> /images/img.jpg"/> /images/img.jpg"/> /images/img.jpg"/>

information output

"; } ?>