Key Takeaways
- Begin by setting up a WordPress installation and creating a new theme folder within `/wp-content/themes/` to house your custom theme.
- Convert your existing HTML and CSS into WordPress format by starting with the `index.php` file and splitting it into separate PHP files like `header.php`, `footer.php`, and `sidebar.php`.
- Utilize WordPress template tags and functions to replace static content in your HTML template with dynamic WordPress content, ensuring your theme pulls information directly from WordPress.
- Add CSS, JavaScript, and image assets to your theme directory, updating paths in your CSS to reflect the new locations within the WordPress structure.
- Implement the WordPress Loop in your `index.php` file to handle the display of blog posts and other dynamic content, customizing it to match the structure and style of your original HTML template.
- Prepare your theme for wider use by adding widget support and custom menu capabilities, ensuring it meets WordPress standards and is ready for distribution or personal use.
WordPress Theme Basics
WordPress themes live in a folder inside the/wp-content/themes/
directory in your WordPress installation. Each theme’s folder includes a few essential components:- A main CSS file. This is just a regular CSS file, except for a few extra details at the top, which I’ll be explaining shortly. You can, of course, have more CSS files if you need to (a print style sheet, for example).
index.php
, the main index file that WordPress loads when you’re using the theme.
index.php
page can also be split up into several other files, which would then be included in index.php
. Normally, these are:header.php
: contains the first part of the template, usually starting from the doctype and extending to just after the page’s header (including the site and page title, and navigation elements).sidebar.php
: similar to theheader.php
file, it includes elements that appear in the sidebar. You can enable this to work with widgets in WordPress, or, if you prefer, you can enter the content directly into the theme files.footer.php
: normally called last in the page, and usually placed from the end of the page content through to the bottom of the web page.comments.php
: defines the structure of the comments section for each post. If you leave this out of your theme, thecomments.php
file from the default theme will be used.
Starting Your Theme
Ideally, before you begin, you need an installation of WordPress up and running, which is available free from WordPress.org. When you’re creating the theme, it’s easiest to work on the files locally or on a local VM, although you could also work on a web server over FTP.First, you need a folder for the theme. This needs to be created inside/wp-content/themes/
in your WordPress installation directory. It’s as simple as creating a new directory with a name related to your theme. Inside this, you’ll start off with your style sheet, which also includes some information about the theme that will be used in the administration panel later.Create a CSS file named style.css
, and add the following at the top of the file:/*Theme Name: [A unique theme name here]Theme URI: [The theme website]Description: [A description]Author: [Your name]Author URI: [Your website].[Any other comments].*/This is a comment block (enclosed by
/*
and */
), but WordPress reads this information and presents it on the theme selection screen in the administration interface.You need to insert content for each of these items. They’re mostly aimed at themes that will be distributed, so if you only plan to use the theme on your own site, most of the values are of little consequence. Make sure the theme’s name differs from any other themes you have installed, or it will cause problems!There’s also the option of adding a version number with the Version:
label.At this point, if you’re converting an existing HTML/CSS site, it should be easy enough to copy and paste all of your style information into this file from your original template’s CSS.index.php
Next up, it’s the index.php
file. The easiestway to begin is by copying and pasting all of the content from the mainHTML file in your site template into this new file.We’ll start by replacing some of the hard-coded information in the file with dynamic content that will be generated on the fly by WordPress.WordPress has a built-in function called bloginfo
for accessing all types of information about the installation and the theme. We’ll use this to fetch the style sheet URL and the location of the RSS feed. bloginfo
is extremely simple to use: <?php bloginfo('stylesheet_url'); ?>In this example, I’ve included the style sheet URL; however, it works for a whole range of parameters including the
charset
, blog description, and template directory. For a full list, see the WordPressCodex.Looking at your template, you now want to replace the link
element pointing to your style sheet with a line like this:Example 1. (excerpt)
<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" />
Everything between
<?php
and?>
will be replaced by the return value of the function, which in this case will be the URL pointing to your style sheet. This is perfect, because your website will now contain a line like: <link rel="stylesheet" href="http://example/blog/wp-content/themes/style.css" type="text/css" />
Assets
Of course, your CSS file most likely references a number of images, so it’s necessary at this point to move those over to your theme directory. While there are no set rules for how you name image and other asset directories within a theme folder, it’s worth adding a new folder calledassets
here, which will include things like images and JavaScript files. If you prefer, you can have separateimages
and js
folders, but for the sake of this article, I’ll assume you’re sticking with a single assets
folder. Move all your images to the new folder.You need to change any references to the old image locations that existed before the template was converted to a WordPress theme to the new locations. Find and replace works in virtually every text editor out there, and is the easiest way to achieve this. Look for references to the old path (for example, images/
up to just before the filename, including the trailing slash), and replace them with the following:<?php bloginfo('template_directory') ?>/assets/This will change all references to the path to the new folder your theme lives in. The
assets
directory is where the images are now housed.If the location of the images relative to the CSS file have changed, a quick find and replace from the old folder name to the new one should make short work of this.Now, you should have a copy of your HTML, CSS, and images all set up and working in WordPress. To check, save it and try setting WordPress to use your theme, and see what happens. You should receive a duplicate of the original HTML template, only now it’s being generated by WordPress. There should be no WordPress content in there just yet, though.The Header, the Footer, and the Sidebar
The next task will be to split the content into the header, footer, sidebar if you have one, and the rest of the page. Bearing in mind that the header, footer, and sidebar code remains unchanged on all pages (as a general rule), start by working from the top of theindex.phpuntil you reach the beginning of your sidebar or your main page content (depending on which one is first in the source), and select everything from the beginning to this point. This will usually include your page title, logo, and navigation menu.Cut and paste this into a new file, and save it in the same location as your
index.phpfile with the name
header.php. The filename is important, as WordPress will go looking for this file when you ask it to insert the header in your pages. Speaking of which, let’s tell WordPress to do this. In the place of the code you cut out of
index.php, put the following line:
<?php get_header(); ?>
code tells WordPress to include the content from your header.phpfile at that location in the page.Next, we’ll do the same for the page’s footer: cut all the footer material from
index.phpand paste it into a new file called
footer.php, replacing it with:
<?php get_footer(); ?>
Finally, do the same action with your sidebar, saving it as sidebar.phpand replacing it with
<?php get_sidebar(); ?>.It’s worth checking again to see that your page is still working at this point. We haven’t made any changes, just split it up into separate files, but it’s good to verify that everything is still working.
Template Tags
At this point, your site is just showing static HTML contained in your template, rather than fetching live data from WordPress. It’s time to change that.In WordPress, you use template tags to tell WordPress what content to fetch and insert into a page. There’s a definitive list of them on the WordPress Codex, but if you’ve been following this far, you’ve already met a few of them!get_header,
get_sidebar,
get_footer, and
bloginfoare all template tags.These tags can be added to any PHP file within the theme, so a good place to start is at the top of your site, with the
header.phpfile.Let’s start at the very beginning of the file. The
Doctypecan remain as is. The opening html tag can include a lang attribute, and WordPress provides this with the
language_attributestemplate tag. So, we can replace the html tag with:
<html xmlns="https://www.w3.org/1999/xhtml" <?php language_attributes(); ?>
This will generate an attribute along the lines of:<html xmlns="https://www.w3.org/1999/xhtml" lang="en-US">
If you’ve included any scripts, it’s worth moving them to the assetsfolder, and changing any references to them as you did for the images. If you used a global find/replace to modify the image paths, it’s possible that the script paths were modified as well, though you’ll still need to move the JavaScript files themselves.For blogs, it’s a good idea to include links to your RSS feed and pingback URL in your
bloginfoby including these lines:
<link rel="alternate" type="application/rss+xml" title="<?php bloginfo('name'); ?> RSS Feed" href="<?php bloginfo('rss2_url'); ?>" /><link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" />
With that done, you now need to include the wp_headtag. This tag will pull in any JavaScript files or style sheets required by the WordPress plugins you’re using. This is vital, as those plugins may fail to function as intended. All you needis the line:
<?php wp_head(); ?>
The final element in the HTML head is the page title. Most WordPress themes use some variation of the following:<title><?php bloginfo('name'); ?> <?php wp_title('-'); ?></title>
This will include the name of the blog, which can be defined in the WordPress settings, followed by the page title. For example, if the page is a single article, it will show the article’s title. The '-'in brackets is the separator, which will be placed before the
wp_titlecontent if (and only if) there’s a title to display. This means that the title of my blog’s home page will be “My Blog,” whereas the title of an article on my blog will be “My Blog: Article Title.” WordPress is smart, and will only include the separator if it’s needed.Still in the
header.phpfile, we’ll now move onto the body tag. This part will vary more depending on the structure of your template, but work through it andlook for any content that should be pulled in from WordPress. For example, if the website title appears in the code as
<h1>My Blog</h1>, it should be replaced with
<h1><?php bloginfo('name'); ?></h1>.The template tags you’re most likely to need at this point are:
-
get_option('home');
: the URL of the blog’s home page -
bloginfo('rss2_url');
: the URL of the blog’s RSS feed -
bloginfo('description');
: the description of the blog, as defined in the WordPress settings
<?php ?>tags; they can sit anywhere inside the PHP file, even inside your HTML tags.Moving your site’s navigation into WordPress can be a bit trickier. There are template tags that can provide you with lists of categories or lists of pages, which you can then use to create parts of your navigation. The template tags are
<?php wp_list_categories(); ?>and
<?php wp_list_pages(); ?>, and will provide you with lists of hyperlinks that you can style as you would a static navigation list. However, sometimes you need your pages to appear in a specific order, or you need to exclude certain pages. Much of this is possible by passing certain parameters to
wp_list_pages. To learn more about how to manipulate this tag,
Widgets
If you plan on releasing your theme to the wider community, it’s important that you widgetize the sidebar. Widgets are chunks of content that can be added to predefined areas in a theme via the WordPress administration panel. For example, you could have a widget area at the top of your sidebar, where the blog owner could easily add a list of pages or an arbitrary block of HTML. Widgets are beyond the scope of this tutorial, but you can read more about them on the WordPress Codex.Other tags you may like to add to your sidebar include (again wrapped in<?php ?>tags):
-
wp_list_authors();
: lists all the blog’s authors asli elements. If they have authored posts, their name will be a link to a page showing all their posts. -
wp_list_bookmarks();
: outputs the links that have been added in theLinks section of the administration panel, and is also wrapped inli tags. -
wp_tag_cloud();
: displays a cloud of all the tags that have been added to your posts.
header.phpfile, such as body or any wrapper divs.As you’ve worked through this, you probably tried viewing the page at various points to test it. Whether or not that’s the case, when you view the page now, it should include all the content being pulled from WordPress, except the main body of the page.
The Main Blog Content
Now that we’ve defined the content for the header, footer, and sidebar, it’s time to turn to our page’s core content. We’ll now go back to ourindex.phpfile and implement the WordPress Loop. The Loop is the mechanism WordPress uses to cycle through the posts that are to be displayed on a given page, and output the content for each of them.It begins like this:
<?php if (have_posts()) : ?><?php while (have_posts()) : the_post(); ?>
These two lines check to see if any posts have been provided. Depending on which page you’re viewing, different posts will be available. For example, on the main blog page, a certain number of the most recent posts will be shown. If you’re viewing a specific post, then only that post will be provided to the Loop. For category, tag, or author pages all posts belonging to that tag, category, or author will be shown.With those two lines in place, we’re now inside the loop. We now need to instruct WordPress to execute a few lines of code individually for each post in the set. For example: <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2><small><?php the_time('F jS, Y') ?> by <?php the_author() ?> </small>
This example section begins with an h2 tag containing the post title, which is linked to that post’s page. Beneath this, the post time and author are listed, wrapped in the small tags. There are a number of new template tags here, and many others you can use to customize your template to display the HTML you want for each. Here are some of the more common ones: -
the_permalink()
: the URL of the permanent hyperlink to the post. -
the_title()
: the post’s title. -
the_time('F jS, Y')
: displays the post’s date in “January 1st, 2010” format. To format the date differently, replace'F jS, Y'
with anotherPHP date format string. -
the_author()
: displays the name of and links to the archive for the user who wrote the post. -
the_content()
: inserts the post content. There’s no need to place this inside<p></p>
tags, as this will be done automatically. -
the_category()
: displays the name of and links to the archive of the post’s category.
<?php endwhile; ?>. This runs once all the posts have been processed. Ideally, it should be followed by some navigation controls:
<div class="navigation"> <div class="alignleft"><?php previous_posts_link('« Previous Entries') ?></div> <div class="alignright"><?php next_posts_link('Next Entries »') ?></div></div>
Each page displays a certain number of posts, as defined in the WordPress settings. These controls will allow your visitors to navigate back to older posts with Next and Previous links. The parameter passed to previous_posts_linkand
next_posts_link(the string in parentheses) the text to be used for the link.At this point, note that the
whilestructure has been closed, but the
if(have_posts())is still open. I need to elaborate on this a little. It’s possible that a page will have no posts to display (for example, your home page before you’ve added any posts, or an archive page for a month in which no posts were published.) In those cases,
if(have_posts())will evaluate to
false, so none of the template code you’ve just written will be run.For those cases, you’ll want to provide some fallback content. So,we first need to define the
elsecondition, and thenclose the
ifstatement with PHP’s
endifkeyword:
<?php else: ?><p>Sorry, there are no posts to display.</p><?php endif; ?>
For this example, we’ve simply added a paragraph telling the that there were no posts found for the page. You could also add a box or links to other parts of your site, to help visitors find their back to the content they’re looking for.Other Pages
In our simple example,index.phpis being as the template for every page on the site. But if you want to modify aspect of the template only on a specific page (or group of WordPress lets you do that easily. All you need to do is
index.phpwith more specific template files. example, you can create a
single.phpfile, and template will be used for single post pages instead of
index.phpfile. Similarly, there
category.php(for category
search.php(for search results),
home.php(for the home page), and a number of othertemplate files you can create to customize each separate area of yoursite.
What’s Next?
If you’ve followed along with this entire tutorial, you have:- imported the content from your HTML template into your WordPress theme files
- changed the references to images, JavaScript files, and similar within your code
- copied all the CSS files, JavaScript files, and images into your new WordPress theme’s directory
- included a few bits of code to tell WordPress where the different pieces of content go
- added some menus and links that WordPress puts together automatically
- begun familiarizing yourself with the WordPress loop and the ideas behind WordPress theming
Frequently Asked Questions (FAQs) about Creating Your Own WordPress Theme from an HTML Template
What are the prerequisites for converting an HTML template to a WordPress theme?
Before you start converting an HTML template to a WordPress theme, you need to have a basic understanding of HTML, CSS, PHP, and JavaScript. You should also be familiar with the WordPress platform, including its file structure and template hierarchy. Additionally, you’ll need a local development environment set up on your computer, such as XAMPP or MAMP, and a text editor like Sublime Text or Atom.
Can I convert any HTML template into a WordPress theme?
Yes, you can convert any HTML template into a WordPress theme. However, the complexity of the conversion process may vary depending on the complexity of the HTML template. Some templates may require more advanced PHP and JavaScript knowledge to fully convert into a WordPress theme.
How do I handle dynamic content when converting an HTML template to a WordPress theme?
When converting an HTML template to a WordPress theme, you’ll need to replace the static content in your HTML template with dynamic WordPress functions. These functions will pull content from your WordPress database and display it on your website. For example, you might replace a static blog post title in your HTML template with the WordPress function the_title().
How can I add WordPress widgets to my converted theme?
To add WordPress widgets to your converted theme, you’ll need to register widget areas in your theme’s functions.php file. Once you’ve registered a widget area, you can then call it in the appropriate location in your theme files using the dynamic_sidebar() function.
How do I make my converted theme responsive?
To make your converted theme responsive, you’ll need to include a viewport meta tag in your header.php file and use media queries in your CSS. You may also need to adjust the layout and design of your theme to ensure it looks good on all screen sizes.
Can I sell the WordPress theme I create from an HTML template?
Yes, you can sell the WordPress theme you create from an HTML template, as long as you have the rights to the original HTML template. If you’re using a free or purchased HTML template, you should check the template’s license to see if it allows for commercial use.
How do I update my converted theme?
To update your converted theme, you’ll need to make changes to your theme files and then upload them to your WordPress installation. If you’ve made changes to your theme’s functions.php file, you should create a child theme to prevent your changes from being overwritten when you update WordPress.
How do I add a custom menu to my converted theme?
To add a custom menu to your converted theme, you’ll need to register a menu location in your theme’s functions.php file using the register_nav_menus() function. You can then call this menu location in your theme files using the wp_nav_menu() function.
How do I add a blog to my converted theme?
To add a blog to your converted theme, you’ll need to create a new file in your theme directory called home.php. This file will serve as the template for your blog page. You can then use the WordPress loop to display your blog posts on this page.
How do I add custom post types to my converted theme?
To add custom post types to your converted theme, you’ll need to register them in your theme’s functions.php file using the register_post_type() function. You can then create a new file in your theme directory for each custom post type, which will serve as the template for displaying these posts.
Matthew is an experimental designer, has an interest in web design and personal productivity, and manages Slync, a small business undertaking projects for clients around the world. Matthew is also interested in photography, and prehospital care.