WordPress. How to create a page template. Creating various WordPress themes manually and using programs

This article will show you how to create the simplest WordPress theme. Although Codex provides extensive documentation on this subject, I find it a bit complex for a newbie. Therefore, in this “tutorial” I will tell you the principles of how WordPress themes work and show you how to adapt a raw HTML template to them. At the same time, you are not required to know PHP, but it is good if you know Photoshop and CSS to create a design.

1. Blog Frontend

Before we get started, let's take a look at a standard WordPress theme and understand what it consists of. Let's mark the elements (header, post title, search form, navigation, footer, etc.).

Standard Frontpage ( index.php)

Standard Single ( single.php)

2. Photoshop layouts

Based on the look and feel of the standard theme, design Photoshop layouts for your blog. For example, I'm using GlossyBlue, one of my free themes. Download demo.zip to see the finished Photoshop file.

3. HTML and CSS

Once the PSD design is ready, create an HTML+CSS template for each page. By following the steps in this tutorial you can use my GlossyBlue HTML files from demo.zip. After unpacking the archive, you will see index.html, single.html And page.html. Next I will use them to turn them into a template.

Why create static HTML first? This is mainly because it will greatly simplify the development process. Usually I create an HTML file for each template, check their validity (HTML and CSS markup) in all browsers. After that, all that's left is to cut and paste the WordPress code. So you no longer have to worry about errors in HTML or CSS.

4. How does a WordPress theme work?

If you go to the default theme folder ( wp-content/themes/default), you will see many php files (called template files) and one file style.css. WordPress usually uses several template files ( index.php , header.php, sidebar.php, And footer.php).

More details in Codex: “Site Architecture” and “Template Hierarchy”.

5. Duplicate template files

Copy the HTML folder from GlossyBlue to the folder wp-content/themes. After that, go to the default theme directory, copy comments.php And searchform.php to a folder glossyblue.

6. Style.css

Go to the default theme folder, open the file style.css. Copy the commented text at the beginning of the file and paste it into style.css GlossyBlue themes. If you want, you can change the title and author information.

7. File separation

Now we need to figure out where to split the HTML files into parts: header.php , sidebar.php , and footer.php. The screenshot below shows a simplified version of my index file, as well as the principle of its division.

8.Header.php

Open index.html. You need to cut the section from the top to where it ends, paste it into a new php file and save as header.php.
Go to default theme folder, open new header.php. Copy and replace tags where the php code requires it: title, link, style sheets, h1 and div class=description.

Navigation menu (wp_list_pages) Replace tags li V ul id=nav on ;

9. Sidebar.php

Return to index.html, cut the code from where it starts form id=searchform and until the tag closes div id=sidebar, put it in a new php file and save as sidebar.php.

  • Replace form id=searchform with all content on .
  • Replace tags li categories by
  • Replace tags li archives on

10. Footer.php

Return to index.html. Extract the code from there div id=footer inclusive with the tag div id=footer and until the end /html then put it in a new one footer.php.

Recent Posts Here I used query_post to display the 5 latest blog posts.

"Latest comments""Latest comments" generated by the plugin (included in the theme folder)

11. Index.php

Now in your index.html should only stay div id=content. Save the file as index.php. Enter the lines: get_header, get_sidebar, And get_footer in the order they appear in the pattern.

12. Parsing the cycle

The loop displays blog posts sequentially based on your settings. The screenshot below illustrates how it works. Initially, the loop checks for the presence of records and if it does not find any, it displays a message "Not Found".

13. Copying a cycle

Go to the default theme directory, open index.php. Copy the cycle from the standard one index.php in your own - between div id=content../div. After that, replace the static text with WordPress template tags: post date, title, category, comments, next and previous link.

14. Theme preview

Congratulations! You have created the public part (the main part of the template). Now go to the administrative panel, go to the tab Design, you should see the GlossyBlue theme. Activate it and go to the public part to see the result in action.

15. Single.php

It's time to create a template single.php. If you want, you can repeat the steps, transferring the code from the standard theme. But it seems easier to me to use the one I just created index.php, saving it as single.php. Open single.php from the standard theme and copy the template tags to the required places. Next, connect comments_template. The following screenshot shows the changes I made:

16.Page.php

Now new single.php save with name page.php. Remove the date of entry, comment form, next/previous links. That's it, that's all - your template page.php ready .

17. Removing HTML files

Remove all HTML files from the folder glossyblue(we won't need them anymore). Technically, this is enough to create a basic WordPress theme. You may have noticed that the standard theme has more PHP files. Well, actually, you don't really need them if you want a simple theme. For example, if search.php or 404.php will not be in the theme folder, WordPress will automatically use index.php to display the page. Read Template Hierarchy for more details.

18. WordPress Page Template

Now for the final example, I'll show you how to use Page Template to create an Archive page that will contain a list of all the posts on your blog (useful for a sitemap). Copy archives.php from the standard theme folder. Remove the unnecessary code and get something like this:

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

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

So let's get started.

Distributing the code into files

1. Download the template and unpack it into your WordPress themes folder (address like blog_address_wordpress/wp-content/themes/). If you want, rename the theme folder to your liking. For example, my template is located at blog_address_wordpress/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 authorship, version, description and other data.

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

4.1. IN 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 correct file name.

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

4.3. IN sidebar.php copy the side menu code (from By ).

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

5. Remove 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 yet integrated the template with WordPress.

Adapting header

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

The template will contain PHP inserts. This code starts with. Between them is PHP code that most often calls CMS functions

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

to the next one:

> "> "> " 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 specifying the encoding as a constant, we called a function that takes the value from the CMS settings and automatically inserts it into the code, that is, to change the encoding you no longer have to edit the theme file.

An important function that ensures that styles, plugins and scripts work on the page.

2. Proceed to edit the index.php file. At the very beginning, write down

,

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

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

Making the top menu dynamic

For now we have a completely static design theme, including one with an unchanged horizontal top menu. While it cannot be configured from the admin panel, and if you leave the code in its current form, 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 and not to plunge into the abyss of the fascinating world of programming, we will give the dynamic menu a static name. Instead of the horizontal menu table, paste the code

so that it looks like this: