Skip to main content

Creating a WordPress Theme From Static HTML: Creating Template Files

In this blog i will talk about how to convert your html theme into WordPress. In this tutorial you'll learn how to take your index.html file and split it up into a set of template files for use by WordPress.

What You'll Need

For this tutorial all you'll need is the most basic tool for editing HTML and PHP:

A code editor of your choice.

What Are Template Files?

A WordPress theme consists of a number of template files. At the very least, a theme must contain two files for it to work, these are index.php and style.css.

However, in a well written theme, the contents of the index.php file will be split up into the main template file (index.php) and a set of include files. These are the files containing the code for the header, sidebar and footer.

In some themes, an additional include file is used for The Loop; I'll come to that in Part 4 of this series. The files are called include files because you add code to your index.php file to tell WordPress to include their contents.

Our index.html file will be split into four PHP files:

index.php, which will contain the main content plus the code to include the other files
header.php, which will include the section plus everything in the header and navigation
sidebar.php, which will contain the sidebar widget area
footer.php which will contain (you've guessed it!) the footer, plus the closing
tag
But for now, all you're going to do is create a set of PHP files and split the contents of your index.php file to them.

Creating PHP Files

The first step is to create empty files. Create four blank files with the following names and open them in your code editor:

index.php
header.php
sidebar.php
footer.php

Make sure all of these are in a folder which has the name of your theme - in my case I'll name the folder 'wptutsplus-demo-theme'.

Copy your stylesheet into this folder, as well. You won't be editing it in this tutorial, but you will be doing so in the next part of the series.

Populating the Header File

Next you'll copy the top part of index.html into your header.php file.

Open index.html and select everything from the opening DOCTYPE declaration to the opening div class="main" tag:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>WordPress Theme Building - Creating a WordPress theme from static HTML</title>
    <link href="style.css" rel="stylesheet" media="all" type="text/css" />
    <header role="banner">
        <div class="site-name half left"></div>
        <div class="site-name half left">
            <h1 class="one-half-left" id="site-title"><a title="Creating a WordPress theme from static html - home" rel="home">WordPress Theme Building</a></h1>
            <h2 id="site-description">Creating a WordPress theme from static html</h2>
        </div>
         
        <aside class="header widget-area half right" role="complementary">
            <div class="widget-container">This will be a widget area in the header - address details (or anything else you like) goes here</div>
        </aside>
    </header>
    
    <nav class="menu main">php /*  Allow screen readers / text browsers to skip the navigation menu and get right to the good stuff */ ?>
        <div class="skip-link screen-reader-text"><a title="Skip to content" href="#content">Skip to content</a></div>
        <ul>
            <li><a href="#">Home</a></li>
            <li><a href="#">Latest news</a></li>
            <li><a href="#">Featured articles</a></li>
        </ul>
    </nav>
    <div class="main">
Copy this code and paste it into your header.php file.

Save your new header file.

Populating the Sidebar File

Now repeat this for the sidebar.

In your index.html file, select the aside class="sidebar" element and everything inside it:

01
02
03
04
05
06
07
08
09
10
11
<aside class="sidebar widget-area one-third right" role="complementary">
    <div class="widget-container">
        <h3 class="widget-title">A sidebar widget</h3>
        <p>This is a sidebar widget, in your WordPress theme you can set these up to display across your site.</p>
    </div>
    <div class="widget-container">
        <h3 class="widget-title">Another sidebar widget</h3>
        <p>A second sidebar widget, maybe you could use a plugin to display a social media feed, or simply list your most recent posts.</p>
    </div>
</aside>
Now copy this into your sidebar.php file and save it.

Populating the Footer File

The process of populating the footer.php file is identical to the header and sidebar.

Select everything after the sidebar in your index.html file:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</div>
<footer>
    
    <aside class="fatfooter" role="complementary">
        <div class="first quarter left widget-area">
            <div class="widget-container">
                <h3 class="widget-title">First footer widget area</h3>
                <p>A widget area in the footer - use plugins and widgets to populate this.</p>
            </div>
        </div>
        <div class="second quarter widget-area">
            <div class="widget-container">
                <h3 class="widget-title">Second footer widget area</h3>
                <p>A widget area in the footer - use plugins and widgets to populate this.</p>
            </div>
        </div>
        <div class="third quarter widget-area">
            <div class="widget-container">
                <h3 class="widget-title">Third footer widget area</h3>
                <p>A widget area in the footer - use plugins and widgets to populate this.</p>
            </div>
        </div>
        <div class="fourth quarter right widget-area">
            <div class="widget-container">
                <h3 class="widget-title">Fourth footer widget area</h3>
                <p>A widget area in the footer - use plugins and widgets to populate this.</p>
            </div>
        </div>
    </aside>
</footer>
Copy it and paste it into your footer.php file.

Save your footer file.

You might be wondering why the .main div is closed in the footer file and not the sidebar. This is so that if you later set up a template file for pages which don't have a sidebar, you'll miss out the sidebar include in that template and keep the footer include, and the .main div will be closed correctly.
Populating the Index File

The final step is to set up your index.php file. This is slightly more involved, you'll have to add some PHP functions which WordPress uses to include the header, sidebar and footer.

Open your empty index.php file and add the code shown below:







Be sure to leave a space between the opening header include and the sidebar include, it's here where you'll add the contents of the index file which aren't in the header, sidebar or footer.

Now open your index.html file again and select all of the code between the opening div class="main" element and the sidebar:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
<div class="two-thirds" id="content">
    <article class="post" id="01">
        <h2 class="entry-title">This is the title of a post or page</h2>
        <img class="size-large" alt="" src="images/featured-image.jpg" />
        <section class="entry-meta">
            <p>Posted on 5 November by Rachel McCollin</p>
        </section>
        <section class="entry-content">
            <p>This is the content of the post. On an archive page it might be an excerpt of the post or you might include the entire content.</p>
            <h3>Images in WordPress</h3>
            <img class="alignright" alt="" src="images/another-image.jpg" />
            <p>This post has some images included - once you've converted this html to a WordPress theme you'll be able to get WordPress to handle images for you and life will be so much easier!</p>
            <p>It also has a featured image - again, WordPress will handle these for you and you'll never go back to static html again. You'll learn how to add support for featured images to your theme in Part 10 of this series. You can use them to automatically display images in your individual posts and pages and in archive pages, you'll learn how to set up a custom archive page in Part 11.</p>
        </section>
        <section class="entry-meta">
            <h3>Post Categories and Tags</h3>
            <p>In this section you can display information about the categories and tags associated with your post, you'll learn how to do this using WordPress template tags in Part 4 of this series.</p>
        </section>
    </article>
</div>
Copy this and paste it into your index.php file below the get_header() line.

Save your index.php file.

Summary

You now have the beginnings of a WordPress theme. You've converted your HTML file to a set of PHP files and set them up to work together.

In the next part of this series, I'll show you how to edit the stylesheet to make your theme work, and upload your theme to WordPress.

Thank you.

Comments

Popular posts from this blog

15 Amazing Websites You Should Know- Make Your Work Easy

15 Amazing Websites You Should Know Hello Reader, Today i'm going to share some websites which are very useful in our college life and for other stuff which we like to do. Internet have to millions of millions websites and lots of websites names and it's functionalities we don't know.So,here top 15 amazing websites which you should know and use it in your work. So,lets get started. 1.  http://www.zamzar.com/ Zamzar is an online file converter, It allows user to convert files without downloading a software tool, and supports over 1,000 different conversion types. You just need to upload your file and the choose your file in which you want to convert and the hit the submit. You can also use different options like manage files,URL converter and develop API. It's have four simple steps and you get converted file through e-mail.It have almost all the option for file conversion. Here the look of the website. 2.   https://www.mailinator.com/  ...

Interview Related C Programming Questions and Answers

C Programming Interview Questions and Answers Here are some of the most frequently asked C Programming questions in the interview time. Refer this blog further for head "Your Hired!" at the interview time.☺so, let's get star. 1) What is C language? C is a high-level and general-purpose programming language that is ideal for developing firmware or portable applications. Originally intended for writing system software, C was developed at Bell Labs by Dennis Ritchie for the Unix Operating System in the early 1970s. _________________________________________________________________________________ 2) Why C is known as a mother language? C language is considered as the mother language of all the modern languages because most of the compilers, JVMs, Kernals etc. are written in C language and most of languages follows c syntax e.g. C++, Java etc. It provides the core concepts like array, functions, file handling etc. that is being used in many languag...

Basic Introduction of Python (about libraries)

Basic Introduction on Python Hello Guys, Today i'm going to taking about python programming language.I'm going to make some other blog for more on python programming language.So i hope you get something from this blog and if you have any question then ask i will solve your queries. Python is a widely used high-level programming language for general-purpose  programming, created by Guido van Rossum.  Aninterpreted language, Python has a design philosophy which emphasizes code readability (notably using whitespace indentation to delimit code blocks rather than curly braces or keywords), and a syntax which allows programmers to express  concepts in fewer lines of code than possible in languages such C++ or Java.  It is a high-level open source programming language. It is a powerful ,fast and dynamic. It is Object-oriented and Inheritance which is easy to lean. In C programming language or any language when our application or project require s...