How to build a WordPress theme from scratch. How to create your own WordPress theme? Why not use different page templates

In this article, we will learn how to create a wordpress theme. First, we'll create HTML + CSS template, which we put into the structure wordpress themes... After you finish reading this article, you can create your own template for wordpress, or rather, you will create it as you read and complete the practical exercises. You will know how the theme works and you will be able to independently implement the core of the wordpress theme from other HTML / CSS templates. Let's start?

Introduction - wordpress theme structure

The structure of a wordpress theme is extremely simple. In the subject we have a file index.php, he is responsible for assembling the pieces of the template. Others are connected to this file: header.php,footer.php... These files (pieces of the template) are used on any page of the site. We know that not all pages are built according to the same template, so the wordpress theme has other files, such as archives.php or single.php... You can also create your own type of pages if you want it to be completely different from others on the site. It is very convenient.

If you carefully consider the structure of the theme, and read the contents of the files, you will intuitively understand what's what. But there are still some points that we will consider. You can always return to this page and read how everything is done.

Now let's look at creating a template in stages:

Step 1 - style.css

A CSS file is a file of parameters and design solutions for HTML template elements. You must rename the main css file (if you have more than one) to style.css... Next, you need to add the following commented information at the beginning of this file:

/ * Theme Name: Typography Paramount Theme URI: http: // site / Description: An image-less template focusing on Typography..0. General comments / License Statement if any. ... * /

This code will show the topic information for admins. Make sure it is at the beginning of the file, and there are no whitespace characters in front of it!

Next, I created another file called 1. css, and put it in the folder typography-paramount which I created in the theme's wordpress folder. It is necessary to strictly adhere to this structure so that wordpress can see this file.

Step 2 - header and footer

In this step, we will create two files: header.php and footer.php mentioned earlier. Although they are not basic and are optionally included in the theme, they are used in most templates, so we will create them.

Header.php

Let's start with this file. Create a file and give it a name header.php, then paste the submitted code into it and save. This file will be displayed on all pages of our template.

-

Until now, there has been nothing special about a simple html / css theme. But now we will replace some of the elements with wordpress tags.

> <?php bloginfo("name"); ?> <?php wp_title(); ?> ; charset =" /> "type =" text / css "media =" screen, projection "/>

These are just some of the tags I know. You will find a lot more on the official website: codex.wordpress.org.

language_attributes ()- Displays the language type for the tag .

bloginfo ()- Used to display information about the site, all parameters can be found in the WordPress codex.

wp_title ()- Returns the title of the page.

wp_head ()- includes javascript and other necessary elements.

get_option ()- gets the necessary options for working with the database.

Footer.php

Now let's create the so-called "site footer". Create a file footer.php and put the following code in it. This footer will display the year, site name, and rss links.

< ?php wp_footer(); ?>

phpthe_time ("Y ");?>- displays the current year.

- the name of the blog.

- adding a link to the rss feed of the blog.

wp_footer ()- this is necessary for the core of wordpress itself, which adds the elements it needs there.


Step 3 - main wordpress template file

It's time to create the main wordpress template file that will connect the top and bottom of the site. Create a file index.php... This is one of the two required wordpress theme files (the other is style.css). Paste the newly created file with the following code:

This code helps wordpress to get information where to connect header.php and footer.php... Let's add a few more lines between these tags:

">

at | |

Woops ...

Sorry, no posts we "re found.

This code snippet retrieves information about available blog posts and, if any, displays them. If they are not found, then the part of the code is displayed after which lets us know that the blog is still empty.

Also added the tag which displays navigation links so that users can read previously added posts.

An example of how a wordpress theme works

Now that we have a semblance of a theme, let's see an example of how the created wordpress theme works.


We have four files in our theme, this is quite enough for a start. In the next article, we will add the file single.php, which will be shown when displaying a specific post. This file will include, unlike the file index.php.

If you still have questions or misunderstandings about creating a wordpress template (theme), express your concerns in the comments below!

In this article, I will walk you through the ways to create templates for permanent WordPress pages. Each method has pros and cons. But before you start, let's briefly explain what pages are and how they differ from posts.

In WordPress, you can create pages (page) and posts (post). They differ in that the records: get into the feed on the main page; for entries, categories are indicated; records cannot be tree-like, and pages: are used for such content as "About me", "Contacts", "Sitemap"; do not have categories, but have a tree structure. Entries are usually intended for chronological information (by time of addition), and pages for a tree structure are not time-dependent. For example, this article is published as a "entry" under the heading "Code", and links in the header menu lead to the following pages: Functions.

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

Creating pages in WordPress

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

Method 1: a page template through a file with an arbitrary name and connecting it in the admin panel (classic way)

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 will be able to select a "template":

Since WordPress 4.7. such page templates can be created for any post type, 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 template page Template Post Type: post, page, product * /

Advantages:

    Having created one template, we can conveniently apply it to 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 (file servises.php). This is sometimes 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 by the meta field (see WP_Query).

Disadvantages:

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 when developing. 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 on the page for editing a tree-type post, WordPress scans all template files for the line:

Template Name: ***

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

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

When the page is published, the name of the template file or default is written in the _wp_page_template custom field if the template is not 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 through a file with a specific name (template file hierarchy)

When a page is created, a shortcut is set for it (slug, alternative name). It is used in the url page. 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 is equal to contacts as in the picture, then we'll create a page-contacts.php file in the theme. and fill it with the necessary code (you can copy the content from the page.php template file and edit it for yourself). That's it, now when we visit the page, we should see a 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 when developing.

Disadvantages:

The template is created only for one specific page. Depends on the page slug, if it changes the template will not work. If you use ID, then the dependency 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).

Practically useless when writing templates, and even more so plugins. It can be used when editing your site in which the slug or page ID is known in advance.

How it works:

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

  • (any_name) .php (when a page template is used)
  • page- (post_slug) .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 together with the complexity it opens up wide possibilities. Using this method, you can set a template for any page, post, category, any publication on the site or, in general, a group of any publications. See examples with description:

// the filter passes the $ template variable - the path to the template file. // By changing this path we are changing the template file. add_filter ("template_include", "my_template"); function my_template ($ template) (# analogue of the second method // if this is a page with a portfolio slug, use the page-portfolio.php template file // use the is_page () conditional tag if (is_page ("portfolio")) (if ($ new_template = locate_template (array ("page-portfolio.php"))) return $ new_template;) # template for a category group // this example will use a file from the theme folder tpl_special-cats.php, // as a template for categories with ID 9 , the name "Uncategorized" and the slug "php" if (is_category (array (9, "Uncategorized", "php"))) (return get_stylesheet_directory (). "/tpl_special-cats.php";) # template for writing 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 arbitrary 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; )

Such code should be placed in the theme's functions.php file or in a plugin, or connected in some other way. As you can see from the example, during the template_include filter, conditional tags are already working, global variables are set: $ wp_query, $ post, etc.

Advantages:

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

  • It is possible to create a template when writing a plugin.

Disadvantages:

The need to write code and connect it separately (for example, in the theme's functions.php).

Now I will explain how to make a WordPress theme out of a simple HTML template using the design example from that article.

You may need to adapt the design for WordPress for many reasons, for example, you are transferring your once-static website to a CMS, or you like a design that is not yet in the WordPress collection, or you just want to figure out how the themes of this CMS work from the inside.

So let's get started.

Distributing the code to files

1. Download the template and unzip it to your WordPress themes folder (address like wordpress_blog_address / wp-content / themes /). Rename the theme folder to your liking if you like. For example, my template is located at wordpress_blog_address / wp-content / themes / MyTheme /.

2. Rename the file styles.css v style.css.

3. Open style.css in a code editor (such as Notepad ++) and at the very beginning, paste the following lines:

/ * Theme Name: MyTheme Theme URI: http://test1.ru Author: NoName Author URI: http://test1.ru Description: Example of test Version: 1.0.0 License: GNU General Public License v2 or later License URI : http://www.gnu.org/licenses/gpl-2.0.html * /

As you might have guessed, this is service information about the design theme: title, author, description, license, version, etc. You can replace the right parts of the lines with your own, that is, set your own authorship, version, description and other data.

4. Create files header.php, index.php, sidebar.php, footer.php and distribute the code from index.html.

4.1. V header.php copy the code line by line since this file is responsible for the top of the site. styles.css replace in the code with style.css- new valid file name.

4.2. V index.php paste the code of the main block (from the line by line ).

4.3. V sidebar.php copy the side menu code (from on ).

4.4. V footer.php paste the remaining lines (with to the end of the document index.html).

5. Delete index.html.

6. Go to the admin panel and make sure that the MyTheme template appears in the section Appearance -> Themes... You can even try to view or activate it, but nothing good will come of it yet, because we have not integrated the template with WordPress yet.

Adapting the header

Now we will start from a static template to make a dynamic theme, into which WordPress data and settings will be loaded.

There will be PHP inserts in the template. This code starts with... Between them is the PHP code that most often calls CMS functions.

1. Open the header.php file and replace the code contained in it before the block

to the next:

> "> "> "type =" text / css "media =" screen "/> <?php bloginfo("name"); ?>

We made the block dynamic

The code calls a function that returns the language attributes to the container.

">

Instead of writing the encoding as a constant, we called a function that takes the value from the CMS settings and automatically substitutes it into the code, that is, to change the encoding, you no longer have to edit the theme file.

An important function that makes it work on a page with styles, plugins and scripts.

2. Go to editing the index.php file. At the very beginning, write

,

The lines call up the header, sidebar and bottom files of the site.

You can now view or even activate MyTheme. As a result, the browser will display the already familiar template with a static menu and a single page. To make the menu dynamic and customizable, and instead of one page displaying all the materials placed on the site, you need to transform the template further.

Making the top menu dynamic

So far, we have a completely static theme, including one with an unchanged horizontal top menu. While it cannot be configured from the admin panel, and if you leave the code as it is, then to insert / delete / move items, you will have to edit the file each time header.php, which is extremely inconvenient.

In order not to philosophize slyly and not to plunge into the abyss of the fascinating world of programming, we will assign a static name to the dynamic menu. Instead of the horizontal menu table, paste the code

so that you get the following: