Having a basic WordPress theme template is a helpful resource if you’re creating custom themes and a good place to start if you’re new to WordPress theme development. We’re going to be focusing on WordPress theme development and assume that you’ve already managed to get a WordPress site up and running.
Themes are installed inside wp-content/themes/ in your site’s parent directory. A theme needs two files to be valid: index.php and style.css. If we make a directory inside wp-content/themes and it has these two files then we’ll be able to select it from the appearance menu inside our site’s administrative portal.
The next two files we’ll put inside our theme’s directory are header.php and footer.php. These two files will contain our HTML doctype, head and body tags and will get called from within index.php using the get_header() and get_footer() functions. We will need to insert the wp_head() function inside header.php right before the closing head tag.
If we’re using HTML5, we’ll want these two files to look something like the following. Note: we are using the get_template_directory_uri() function here to retrieve our template’s URI so that we can access our theme’s style.css file.
<link rel="stylesheet" href="<?php echo get_template_directory_uri() ?>/style.css"/>
<?php wp_head(); ?>
Now we are ready to work on our index.php file. The content of this file will be the first thing we see when we go to our site. We’re going to breakdown the following PHP code to learn the basics of WordPress’s ‘The Loop’, the system in which posts can be queried and displayed. We’ll omit HTML tags from this example for clarity, but keep in mind that would be the next step.
if (have_posts()) : while (have_posts()) : the_post();
The first line is using the query_posts() function to query all of our posts for the parameters set within the parentheses. These parameters can be read like so: get seven posts in ascending order from category 5. You can find out your category’s ID by selecting it in the categories menu inside your site’s administrative portal, it will be displayed inside the URL and should look something like taxonomy=category&tag_ID=5.
The second line is our loop. It starts off with the have_posts() function which is true or false depending on if there are posts or not. If true, while (have_posts()) will cycle through each post and from this loop we can use different functions to grab whatever we want from each individual post. The following is a list of some of the basic functions you can use within the loop.
All of the PHP functions we’ve been using are the standard WordPress functions. You can find the entire function reference here: http://codex.wordpress.org/Function_Reference
About Canadian Web Hosting
Since 1998, Canadian Web Hosting has been providing on-demand hosting solutions that include Shared Hosting, Virtual Private Servers (VPS), Cloud Hosting, Dedicated Servers, and Infrastructure as a Service (IaaS) for Canadian companies of all sizes. Canadian Web Hosting is SSAE 16 Type II SOC 2 certified, ensuring that their processes and business practices are thoroughly audited against industry standards. Canadian Web Hosting guarantees a 100% network uptime, and a total money-back guarantee that backs everything they do. Customers can get help by calling 1-888-821-7888 to get 24/7 support. For more information, visit them at www.canadianwebhosting.com, or get the latest news by following them on Twitter at @cawebhosting.