In the first part of this series, I showed you how to prepare your 
HTML and CSS files for WordPress, ensuring the structure would work, the
 code was valid and that the correct classes were being used.
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.
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:
tag
Over the course of this series, you'll add to these files so that 
your theme can include widgets, menus and a loop, and you'll add hooks 
which will be used by plugins. You'll also add extra template files to 
display different types of content. If you want to get a head start on 
this, have a look at the Codex page on the WordPress Template Hierarchy.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 
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
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 |     <metacharset="utf-8"/>    <metaname="viewport"content="width=device-width"/>    <title>WordPress Theme Building - Creating a WordPress theme from static HTML</title>    <linkhref="style.css"rel="stylesheet"media="all"type="text/css"/>    <headerrole="banner">        <divclass="site-name half left"></div>        <divclass="site-name half left">            <h1class="one-half-left"id="site-title"><atitle="Creating a WordPress theme from static html - home"rel="home">WordPress Theme Building</a></h1>            <h2id="site-description">Creating a WordPress theme from static html</h2>        </div>                 <asideclass="header widget-area half right"role="complementary">            <divclass="widget-container">This will be a widget area in the header - address details (or anything else you like) goes here</div>        </aside>    </header>        <navclass="menu main">        <divclass="skip-link screen-reader-text"><atitle="Skip to content"href="#content">Skip to content</a></div>        <ul>            <li><ahref="#">Home</a></li>            <li><ahref="#">Latest news</a></li>            <li><ahref="#">Featured articles</a></li>        </ul>    </nav>    <divclass="main"> | 
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 | <asideclass="sidebar widget-area one-third right"role="complementary">    <divclass="widget-container">        <h3class="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>    <divclass="widget-container">        <h3class="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> | 
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>        <asideclass="fatfooter"role="complementary">        <divclass="first quarter left widget-area">            <divclass="widget-container">                <h3class="widget-title">First footer widget area</h3>                <p>A widget area in the footer - use plugins and widgets to populate this.</p>            </div>        </div>        <divclass="second quarter widget-area">            <divclass="widget-container">                <h3class="widget-title">Second footer widget area</h3>                <p>A widget area in the footer - use plugins and widgets to populate this.</p>            </div>        </div>        <divclass="third quarter widget-area">            <divclass="widget-container">                <h3class="widget-title">Third footer widget area</h3>                <p>A widget area in the footer - use plugins and widgets to populate this.</p>            </div>        </div>        <divclass="fourth quarter right widget-area">            <divclass="widget-container">                <h3class="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> | 
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:
| 
1 
2 
3 
4 |  | 
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 | <divclass="two-thirds"id="content">    <articleclass="post"id="01">        <h2class="entry-title">This is the title of a post or page</h2>        <imgclass="size-large"alt=""src="images/featured-image.jpg"/>        <sectionclass="entry-meta">            <p>Posted on 5 November by Rachel McCollin</p>        </section>        <sectionclass="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>            <imgclass="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>        <sectionclass="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> | 
get_header() line.Save your index.php file.
 
No comments:
Post a Comment