How To Create A WordPress Theme From A HTML Template Part 1 – Introduction
This is the first part of a longer tutorial on how to create a WordPress Theme from a HTML Template. Just by following along with the steps in the next few parts, you will learn how to turn a HTML template into a fully functional WordPress theme.
But if you don’t know what you are working with, and all you do is copying and pasting bits of code you can’t differentiate, it is easy to make mistakes and exceedingly difficult to fix them by yourself.
For that reason, in this part we will talk about key parts of WordPress as a whole, and introduce the different working parts of a WordPress Theme.
Before you even get started reading through this introduction, ideally you would be familiar with HTML/CSS and at least have some basic PHP understanding.
The WordPress Loop
Let’s start out with the thing that powers all of WordPress: The WordPress loop, also known as just “The Loop”, is a block of PHP code used to display posts or pages in WordPress.
Using PHP’s alternative syntax, the loop looks like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts were found.' ); ?></p>
<?php endif; ?>
This the normal way of writing the loop these days. But when using the more old fashioned way of coding it out, the loop looks like this:
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
} // end while
} // end if
else {
_e( 'Sorry, no posts were found.' );
}
?>
The loops calls on pre-written PHP functions within WordPress that do certain things.
Have posts pings the relevant database, and asks it if there is any content available. Because it uses a PHP if clause, if there is content available, it moves on to the next step, whereas if there is no content available nothing happens, it plays through the else code instead. In this case, it simply echoes out the text “Sorry, no posts were found.”
Using a while clause in PHP creates a loop that keeps doing something as long as the condition you specified is true. In this case, the condition we have specified is “have_posts”, meaning that as long as there are posts in the database, it will keep looping through the code within the clause.
the_post, calls another PHP function within WordPress, that simply echoes out the relevant post from the database. Because the_post is inside the while loop, it will keep echoing out posts until no more posts are found for the query made by the have_posts function.
You can read more about the loop here, and see more complicated examples of what is possible to do with it.
The Core Files Of A WordPress Theme
Next let’s take a look at the absolutely required core files of standalone WordPress Theme. When you make a child theme, you only need style.css and functions.php. When you make a completely new theme, in theory you just need one more file, indexx.php.
Index.php – Complete With The Loop
This is the default fallback page for all possible pages a person might visit on your WordPress site. If there are no page.php or post.php files available, this is the template it will use to serve up the content on a url that is visited.
Most themes however, make ample use of many templates within in the WordPress template hieararchy.
Functions.php File
This is where you will pull in necessary outside scripts, and register functionality that exists in your WordPress theme. For example, if you want to work with the WordPress Customizer, you need to do that from within the functions.php file, as well as your actual template files.
Later in the series, you will see exactly how to add “header image”, and other customizable aspects to your WordPress theme, and make full use of the WP Customizer as a developer.
Style.css
WordPress templates like most modern websites rely heavily on CSS to control the and result of their look. And in this file is where the CSS in your theme will be. (If you don’t know what CSS is, you should probably at least go through the HTML/CSS course on Code Academy before continuing with this guide.)
Even if you prefer to use SASS, you will still need to compile it into a regular style.css file before you can work.
Basic Site Architechture
Rather than building each different page template from scratch, WordPress themes typically work with different building block templates, that you piece together and then build on top of. The bare minimum of these is the header.php, sidebar.php, and footer.php files. The thinking behind this is that these parts require fairly substantial bits of code, and are likely to be included in almost every page template. You also get very cleanly separated code, so that it is easier to edit down the road.
All you need to do to call in a template is use the “get_” function. If you want to call in the sidebar.php file in a certain place in your page template, all you need to do is type this snippet:
<?php get_sidebar; ?>
You can also create multiple sidebar, header or footer templates with different names. But make sure to follow this naming convention “header-ADDEDWORD.php”. For example, you might create a “header-fullwidth.php” to show a different header on full width pages. Calling this file would look like this:
<?php get_header('fullwidth'); ?>
But many modern themes go a lot deeper than this, and even have different template-parts, for their different templates for different parts of the page. The newest official WordPress theme, Twenty Seveteen, for example, does this.
This means that instead of calling the WordPress functions for sidebar and content in the actual template files, they are called in the different part files. Then you simply call those part files from your template files. Then finally call the templates into your different page templates.
You can read more about site architecture in WordPress here.
The Template Hierarchy
The template hierarchy refers to the way that WordPress decides which template to use to serve content on a certain permalink. Basically there are certain template files that WordPress prioritizes differently in different situations. Usually this is determined by which permalink the visitor is on and what content is supposed to be displayed.
As an example, to show the way the template hierarchy works: ?If a visitor goes to the permalink of a single post, WP will first look for the single-post.php file. That’s the template file designated to showing standard single posts, so that has first priority.
If there is no single-post.php file, WordPress will look for a single.php file. That is the file used for displaying any kind of single post, custom or standard.
If there is no single.php file, it will look for a singular.php file. That is the file used for displaying any kind of single post or page.
Finally if there is no singular.php file, WP will default to the index.php file, which is the last resort used for displaying all content.
The process is the same no matter what post or page the visitor visits. For example, if a user visits the front page, it looks for a front page.php file. Failing that, if it is a single page displayed, it pulls in the relevant page template file. If posts are displayed it looks for the home.php file. Finally, if there is no home.php file, WP defaults to index.php again. I’m sure you’re starting to understand exactly why an index.php file is absolutely necessary.
You can see a full visual representation for how WP does this, over at wphierarchy.com.
Let’s Get Right Into It
Now that you are familiar with most of the basic working parts of a WordPress theme, let’s jump right in. Starting in the next part, we will create a very basic WordPress theme that uses all of the CSS styles of the original HTML template.
Related Posts
Reader Interactions
Droppin' design bombs every week! 5,751 subscriber so far!
Very readable unlike official WP source & great intro to WordPress themes couple – of typos including :
….theory you just need one more file, indexx.php. [index.php]
…………….it looks for a front page.php file. [front-page.php]
I had a go at wp theme using bootstrap https://github.com/captain-sensible/wp1 example use: bimschoolgh.com biggest problem is understanding how to override say underscore theme with bootstrap. if you look at style.css of underscore its difficult to see how it styles ten there is body_class() which adds style class to body element.
I love how clear and straightforward this intro is structured! I’m a bit fuzzy on some parts of WordPress even though I know that it’s not as insanely complex as it seems.
That again was no use: he but got another smile and a friendly look of the sort he no longer wanted. I said I thought I could gallop if Harry could, and in a few minutes we were up with the ambulance. It had stopped. There were several men about it, including Sergeant Jim and Kendall, which two had come from Quinn, and having just been in the ambulance, at Ferry's side, were now remounting, both of them openly in tears. "Hello, Kendall." We have this great advantage in dealing with Plato—that his philosophical writings have come down to us entire, while the thinkers who preceded him are known only through fragments and second-hand reports. Nor is the difference merely accidental. Plato was the creator of speculative literature, properly so called: he was the first and also the greatest artist that ever clothed abstract thought in language of appropriate majesty and splendour; and it is probably to their beauty of form that we owe the preservation of his writings. Rather unfortunately, however, along with the genuine works of the master, a certain number of pieces have been handed down to us under his name, of which some are almost universally admitted to be spurious, while the authenticity of others is a question on which the best scholars are still divided. In the absence of any very cogent external evidence, an immense amount of industry and learning has been expended on this subject, and the arguments employed on both sides sometimes make us doubt whether the reasoning powers of philologists are better developed than, according to Plato, were those of mathematicians in his time. The176 two extreme positions are occupied by Grote, who accepts the whole Alexandrian canon, and Krohn, who admits nothing but the Republic;115 while much more serious critics, such as Schaarschmidt, reject along with a mass of worthless compositions several Dialogues almost equal in interest and importance to those whose authenticity has never been doubted. The great historian of Greece seems to have been rather undiscriminating both in his scepticism and in his belief; and the exclusive importance which he attributed to contemporary testimony, or to what passed for such with him, may have unduly biassed his judgment in both directions. As it happens, the authority of the canon is much weaker than Grote imagined; but even granting his extreme contention, our view of Plato’s philosophy would not be seriously affected by it, for the pieces which are rejected by all other critics have no speculative importance whatever. The case would be far different were we to agree with those who impugn the genuineness of the Parmenides, the Sophist, the Statesman, the Philêbus, and the Laws; for these compositions mark a new departure in Platonism amounting to a complete transformation of its fundamental principles, which indeed is one of the reasons why their authenticity has been denied. Apart, however, from the numerous evidences of Platonic authorship furnished by the Dialogues themselves, as well as by the indirect references to them in Aristotle’s writings, it seems utterly incredible that a thinker scarcely, if at all, inferior to the master himself—as the supposed imitator must assuredly have been—should have consented to let his reasonings pass current under a false name, and that, too, the name of one whose teaching he in some respects controverted; while there is a further difficulty in assuming that his existence could pass unnoticed at a period marked by intense literary and philosophical activity. Readers who177 wish for fuller information on the subject will find in Zeller’s pages a careful and lucid digest of the whole controversy leading to a moderately conservative conclusion. Others will doubtless be content to accept Prof. Jowett’s verdict, that ‘on the whole not a sixteenth part of the writings which pass under the name of Plato, if we exclude the works rejected by the ancients themselves, can be fairly doubted by those who are willing to allow that a considerable change and growth may have taken place in his philosophy.’116 To which we may add that the Platonic dialogues, whether the work of one or more hands, and however widely differing among themselves, together represent a single phase of thought, and are appropriately studied as a connected series. Before entering on our task, one more difficulty remains to be noticed. Plato, although the greatest master of prose composition that ever lived, and for his time a remarkably voluminous author, cherished a strong dislike for books, and even affected to regret that the art of writing had ever been invented. A man, he said, might amuse himself by putting down his ideas on paper, and might even find written178 memoranda useful for private reference, but the only instruction worth speaking of was conveyed by oral communication, which made it possible for objections unforeseen by the teacher to be freely urged and answered.117 Such had been the method of Socrates, and such was doubtless the practice of Plato himself whenever it was possible for him to set forth his philosophy by word of mouth. It has been supposed, for this reason, that the great writer did not take his own books in earnest, and wished them to be regarded as no more than the elegant recreations of a leisure hour, while his deeper and more serious thoughts were reserved for lectures and conversations, of which, beyond a few allusions in Aristotle, every record has perished. That such, however, was not the case, may be easily shown. In the first place it is evident, from the extreme pains taken by Plato to throw his philosophical expositions into conversational form, that he did not despair of providing a literary substitute for spoken dialogue. Secondly, it is a strong confirmation of this theory that Aristotle, a personal friend and pupil of Plato during many years, should so frequently refer to the Dialogues as authoritative evidences of his master’s opinions on the most important topics. And, lastly, if it can be shown that the documents in question do actually embody a comprehensive and connected view of life and of the world, we shall feel satisfied that the oral teaching of Plato, had it been preserved, would not modify in any material degree the impression conveyed by his written compositions. breakfast in the kitchen by candle-light, and then drove the five The bargaining was interminable, something in this manner:— Then follows a long discussion in Hindi with the bystanders, who always escort a foreigner in a mob, ending in the question— There was a bright I. D. blanket spread on the ground a little way back from the fire, and she threw herself down upon it. All that was picturesque in his memories of history flashed back to Cairness, as he took his place beside Landor on the log and looked at her. Boadicea might have sat so in the depths of the Icenean forests, in the light of the torches of the Druids. So the Babylonian queen might have rested in the midst of her victorious armies, or she of Palmyra, after the lion hunt in the deserts of Syria. Her eyes, red lighted beneath the shadowing lashes, met his. Then she glanced away into the blackness of the pine forest, and calling her dog to lie down beside her, stroked its silky red head. The retreat was made, and the men found themselves again in the morning on the bleak, black heath of Drummossie, hungry and worn out, yet in expectation of a battle. There was yet time to do the only wise thing—retreat into the mountains, and depend upon a guerilla warfare, in which they would have the decided advantage. Lord George Murray now earnestly proposed this, but in vain. Sir Thomas Sheridan and other officers from France grew outrageous at that proposal, contending that they could easily beat the English, as they had done at Prestonpans and Falkirk—forgetting that the Highlanders then were full of vigour and spirit. Unfortunately, Charles listened to this foolish reasoning, and the fatal die was cast. "They said they were going for our breakfast," said Harry. "And I hope it's true, for I'm hungrier'n a rip-saw. But I could put off breakfast for awhile, if they'd only bring us our guns. I hope they'll be nice Springfield rifles that'll kill a man at a mile." "Dod durn it," blubbered Pete, "I ain't cryin' bekase Pm skeered. I'm cryin' bekase I'm afeared you'll lose me. I know durned well you'll lose me yit, with all this foolin' around." He came nearly every night. If she was not at the gate he would whistle a few bars of "Rio Bay," and she would steal out as soon as she could do so without rousing suspicion. Boarzell became theirs, their accomplice in some subtle, beautiful way. There was a little hollow on the western slope where they would crouch together and sniff the apricot scent of the gorse, which was ever afterwards to be the remembrancer of their love, and watch the farmhouse lights at Castweasel gleam and gutter beside Ramstile woods. "Yes, De Boteler," continued the lady, "I will write to him, and try to soothe his humour. You think it a humiliation—I would humble myself to the meanest serf that tills your land, could I learn the fate of my child. The abbot may have power to draw from this monk what he would conceal from us; I will at least make the experiment." The lady then, though much against De Boteler's wish, penned an epistle to the abbot, in which concession and apologies were made, and a strong invitation conveyed, that he would honour Sudley castle by his presence. The parchment was then folded, and dispatched to the abbot. "A very pretty method, truly! You know not the miners and forgers of Dean Forest!—why I would stake a noble to a silver-penny, that if you had discovered he was hidden there, and legally demanded him, he would be popped down in a bucket, to the bottom of some mine, where, even the art of Master Calverley could not have dragged him to the light of day until the Forest was clear of the pack:—but, however, to speak to the point," perceiving that the steward's patience was well nigh exhausted—"I saw Stephen Holgrave yesterday, in the Forest." HoME欧美一级 片a高清
ENTER NUMBET 0016gcqjwc.com.cn kmomjjy.com.cn qhdrohe.com.cn wcchain.com.cn www.sxfkch.com.cn www.ohpkus.com.cn www.pfchain.com.cn sytgroup.com.cn www.sxzesf.com.cn wztesr.com.cn
Nice article, very clear.
I can’t wait for the next parts.
Very readable unlike official WP source & great intro to WordPress themes couple – of typos including :
….theory you just need one more file, indexx.php. [index.php]
…………….it looks for a front page.php file. [front-page.php]
I had a go at wp theme using bootstrap https://github.com/captain-sensible/wp1 example use: bimschoolgh.com biggest problem is understanding how to override say underscore theme with bootstrap. if you look at style.css of underscore its difficult to see how it styles ten there is body_class() which adds style class to body element.
I love how clear and straightforward this intro is structured! I’m a bit fuzzy on some parts of WordPress even though I know that it’s not as insanely complex as it seems.