Pinegrow WP is a web editor for creating WordPress templates. Editing a WordPress template: how to change the theme to suit your needs
Editing a template is difficult, but necessary. I’ve been thinking for a long time about changing the template to another, as I wanted some space, but without bells and whistles. But I didn’t want to take the one I liked and install it, so I decided to delve deeply into its template code, correct it a little: remove unnecessary elements, add the necessary working code. In addition, I want to make my template easier by speeding up its loading. For me, this is the most difficult task, since sometimes I have to cut to the quick and remove a lot of plugins that made my life easier.
To edit the template, I have already laid the foundation, now I have to take the first file for development. I will open the index.php file, since it works like a skeleton in the installed WordPress template: it indicates to all other files the order and time of loading into the user’s browser. Having opened this file with Notepad++, I pay attention to the first line.
I see the following code
1 |
It means that the header.php file is loaded first when generating a blog page. Please note that there is nothing written before this line. Now, to edit and adapt the template, I open the header.php file.
At the very beginning of work, I look at the number of lines in the files: in the index.php file there are 49 lines, but 17 are empty, and in the header.php file there are 58 lines, of which 31 are empty. When I finish editing the template, I will remove all the empty lines, the browser will save time when loading the page when reading the codes - this is such a minimal work on adapting the template for the blog.
Before editing the index.php and header.php files, I change the document encoding to UTF-8, I talked about this in the previous article “Editing a WordPress template”
Line 1 – very important for document verification, it confirms that the page is written in the correct language, guided by accepted standards. I’m not worried about this line – it is and let it be. I'm moving on.
I'm paying attention to line 2 - browser
Below this code is a large container containing 8 lines that I also have to deal with.
I draw your attention to line 4,
1 | ; charset= " /> |
which I will replace with the string
1 | < meta http- equiv= "Content-Type" content= "content=" text/ html; charset= UTF- 8 " /> |
It tells browsers and search engines that the pages on this blog are written using UTF-8 encoding, but again, why would I need the extra load.
We continue to further optimize the template - line 5 or title of the page. It is very important for search engine optimization of a blog. Therefore, I am obliged to use it, but here, too, its filling is implemented using PHP code. This is how this code works
And now a small digression. The fact is that I use the All in One SEO Plugin for search engine optimization of my blog. With its help, I write all the title, description, and keywords for the blog itself, and for each individual page. Here is the code
First part of the code
pulls the post title from the blog database, the second part of the code
Now when I open a blog page, the title of the post will first be displayed in the title, separated by the blog name. The same thing, but minus one more request.
Line 6 contains the file connection
I recently wrote a note about him. I must write the path to this file, starting with the domain name.
In your version, you can only change the address if you edit the blog template for yourself.
Line 8 – must specify the absolute path to the xmlrpc.php file.
It is short, seemingly harmless, but when the main blog page is generated, it will load a lot of code: page navigation plugin styles, captcha plugin, All in One SEO Plugin, wp-syntax, Google Analytics, topsy. You can load properties and styles of other installed plugins. In addition, working scripts are output from the CMS WordPress wp-includes folder.
There is only one conclusion. I have to evaluate the need for each such plugin for the blog, and decide whether it is needed or put under the knife. As an example, I can’t do without the All in One SEO Plugin; I spent a lot of time looking for the wp-syntax plugin. They load my blog, but the return on them is great.
But without WordPress plugins
Each design theme has a number of settings that can be changed from the CMS control panel. The variety of parameters may vary from template to template, but almost every theme allows you to customize the logo, background, fonts, sidebars, title, and description of the site from the admin panel.
Despite this, the site owner may want to change some little things that the control panel does not allow. In this case, you can hire a webmaster, but if you don’t have money or have time, you can easily figure out the template and make the necessary changes yourself.
The engine theme consists of many files. Knowing what part of the site each of them is responsible for, you can easily edit the design of the CMS template to suit your needs.
The theme files contain HTML, PHP and CSS code. There is a separate reference book on HTML/CSS on the site, but PHP knowledge is basically not required to create a design.
So, any WordPress template has the following files.
1. index.php. The file forms the main page of the site and calls other theme files.
2. header.php. Creates the top, “header” of the site - usually it contains a logo, title, description of the web resource, as well as a horizontal menu. HTML container is also in this file.
3. footer.php. Contains the code for the bottom part of the site, its “footer”.
4. style.css. Cascading style sheet file. Since it is usually quite large, style.css well commented. Unfortunately, most often in English, but basic knowledge will be enough to understand which part of the code is responsible for the design of which elements. Read more about CSS here.
This is a required base for a WordPress template, but there are usually many more theme files, and here are the most common ones.
1. single.php- separate post.
2. page.php- page.
3. sidebar.php- side panel/panels.
4. archive.php- archives of articles.
5. search.php- search results page.
6. comments.php- output of comments.
7. 404.php- error page with code 404 (File not found).
8. function.php- a file containing theme functions. You can add your own PHP scripts to it.
Of course, templates usually contain many more files than described above, but they usually do not need editing. In addition, you can learn about the purpose of each file from its name and comments inside.
Editing templates is often required to correct or add some small detail. Below are a few common situations.
Add menu
Menus can be placed not only in places set by the theme (this is done through widgets), but also in any other part of the site or even on a specific page.
To insert a menu anywhere, add the line:
directly into the page code where you want to place it. The line will work if there is only one custom menu on the site. If there are several, you should add a line like this to the code:
"Menu_1")); ?>
where instead of Menu_1 you need to enter the name of your menu.
Change 404 page
If the link leads to a non-existent page or file, WordPress takes the user to an error page with the code 404 (File not found). The file is responsible for it 404.php, stored in the active theme folder. Often English text is displayed on the screen, and the desire of the owner of a Russian-language site to translate it into his native language is quite understandable.
To change the contents of the page, open the file in the editor 404.php, find the lines with the text displayed in the browser and change them (the displayed text is most often enclosed in quotes). For example, in one of the standard themes (twentyfourteen) you will be interested in the following code:
Replace the text in quotes (not where twentyfourteen is, but something else) with your own and get a modified 404 page.
Register copyright
At the bottom of the site it is customary to write official information, in particular, the years of operation of the resource and copyright. In the vast majority of cases, your “label” in the file footer.php left by the authors of the themes, and webmasters, naturally, want to replace this information with their own.
As we know, the “legs” of the site are responsible for the file stored in the root of the active theme footer.php, and you should look for the code responsible for copyright in it.
For example, the flat-white theme hides supporting information in the following lines of this file:
" title=""> ">
Russify text
Often, WordPress templates are not fully Russified or not translated at all. Knowing the file structure of the theme and the fact that the text shown to the user (if it is not a variable) in the code is always enclosed in quotes, it is easy to find and independently translate the desired fragment, and it is not necessary to search for the word manually - for this you can use the automatic search, which is present in every code editor.
For example, in the flat-white template you immediately want to Russify at least two inscriptions: “READ MORE” and “Leave a Comment”, since they are visible on the main page.
The file is responsible for the main page index.php, located in the root of the theme directory, therefore, you need to edit it. But there is no text for the inscriptions, so you need to understand the code to find out where they are stored. In our case, the file is responsible for the information under the posts on the main page template-parts/content.php, connected by string
If you go into it and in the code
text Leave a Comment replaced by
The last extension we need to consider is templates. Exactly the template in CMS Joomla 3, and in any other version determines the appearance and appearance of the site. Thanks to templates, you can radically change the design of your website, spending a minimum of time on it; all you need to do is download and install a ready-made template, and decide on the location of the modules.
But before you start installing a new template, you should figure out what, or rather, what files Joomla 3 templates consist of.
What does a Joomla 3 template consist of?
For templates in Joomla there is a special folder “templates”, in the root of which each template has its own folder of the same name. Without exception, all templates consist of files, and the number of these files may differ for different templates. But there are also standard files, without which not a single template will work:
- index.php– index file, it determines the structure of the site
- templateDetails.xml– this file contains all the information about the template and is used to install the template through the control panel
- Style files (.css)– with their help, the design of pages is determined
In addition, templates may contain script files (.js), images, languages and much more. The screenshot below shows a set of files that make up the standard “Protostar” template:
What are the templates?
There are quite a large number of types of templates; they can be classified according to the frameworks in which they are written or other parameters, but all types of templates can be divided into two large categories:
- Website templates– change the appearance and design of the site
- Control Panel Templates– this type of templates is intended specifically for the control panel; they do not affect the site in any way.
Working with Joomla 3 templates
We’ve sorted out the types, storage location and template files, now let’s talk about how to work with templates using the control panel. First, let's go to the "Templates: Styles (Site)" section ("Extensions" -> "Templates") and see which templates are already present in the basic assembly of CMS Joomla:
On the “Templates: Styles (Site)” page you can see that we have at our disposal two templates Beez3 and Protostar, the latter is set by default for all pages of the site, pay attention to this because in Joomla the same site can consist of several templates. And you can assign your own template to different sections of the site, thereby making the site more attractive.
Let's open one of the templates for editing and see what can be edited and how:
On the first tab with the name “Details” we see some information regarding this template, and then the developers write to us that the template uses the Bootstrap framework.
The next tab “Advanced settings” contains the basic settings of the template, here we can change the color of the text and background of the site, logo, title fonts and other settings:
And the last tab is “Menu Linking”, with its help you can assign a template for the entire site or for specific sections. We simply mark those menu items for which this template will be defined:
Positions of modules in a Joomla 3 template
In the lesson on Joomla modules, we said that they are displayed on the site in strictly defined positions, the so-called module positions. For each specific template, the number and location of these positions is different.
How to find out or where to look at module positions in Joomla 3? Joomla provides a special preview for this, but by default it is not available (turned off) and must be enabled in the settings.
In order to enable the display of module positions in Joomla 3, on the “Templates: Styles (Site)” page, click on the “Settings” button. After this, we will be taken to the “Template Manager Settings” page, on which we set the “View module positions” switch to the “Enabled” position:
After that, go again to the template manager page – “Templates: Styles (Site)” and click on the icon with an eye located just to the left of the template name:
On the page that opens, you can see all the module positions available in the template and their location. In the future, this information will help to display certain modules in them.
Creating your own module positions
We have already learned how to view existing module positions, but what if the template does not contain exactly the position we need? In this case, you can create your own position for modules in a ready-made Joomla template. The process is not complicated, but it does require editing certain template files − index.php And templateDetails.xml.
In the first file we indicate where the new position for the modules will be located, in the second we will tell the engine that this position is present in the template and can be worked with.
Let's move from words to action, first open the file templateDetails.xml, which is located in the template's root directory. There is no point in delving into its essence now, we are only interested in the positions for the modules, they are declared between the lines<position>module-position-nameposition> (lines 28-47 of the Protostar template). By default there are 18 of them:
In order to declare our position of modules, add an additional line (or several lines) of this type:
We save the file, you can close it, the main thing is to remember the name of the position under which you declared it.
Now open the index.php file and add a new DIV block to it, which will be a container for the new position of the modules. As an example, I decided to create a new position for modules in the footer of the site. Write the following code in the footer:
Div>