Here is what we’re going to build together today. Click the image to view the live demo.
We could have created our theme from scratch, but there’s no need to reinvent the wheel when there’s tools that can help you save lots of time. The tool we’re going to use is called Blank Theme. It is a functional WordPress theme, but without any styling. That way, it can be used as a starting point for creating your own themes. For example, I’ve used Blank Theme to create the current Cats Who Code theme.
Blank Theme has been created by Chris Coyier and it can be downloaded
As we’re going to create a theme that will rely much on jQuery, the first thing we have to do is to link to jQuery and the jQuery Mobile files. You can either download the files individually and link them to the theme, or you can use jQuery’s CDN and link the online versions to your theme. I personally prefer linking to the online version, but it’s up to you.
Open the header.php file from the Blank Theme directory, and insert the following code to link to jQuery Mobile, within the
and tags:Once done, the required jQuery files are now linked to our theme. But we’re not done yet with header.php. As jQuery Mobile uses HTML5, we have to modify the doctype. Replace the first 6 lines of the file with:
Then, scroll down to the bottom of the file and locate the
tag. Replace everything from the tag until the end of the file by the following:>We just dove in jQuery Mobile with the code above. What? We haven’t even wrote a single line of JavaScript! That’s right. jQuery Mobile doesn’t need you to write any JavaScript. All it needs is some
As an explanation, take a look at line 3 of the code above. Have you noticed the data-role="header"? It describe a header bar. If you save the file and view your theme right now, you’ll notice a header bar on the top of the screen, with your blog name.
Now, save header.php and open footer.php. Replace its content by:
This simple code will insert a footer bar to our theme, with a button that will scroll up to the header when the user will tap on it. Did you noticed the data-icon attribute? It allows you to specify which kind of icon you want. In our case, we want an up arrow, but there’s lots of different icons you can use.
Step 3: The homepageNow, let’s code our homepage. As we’re building a theme for mobile devices, we do not need anything fancy. Let’s build a list of our recent posts. To do so, open the index.php file, locate the loop, and replace it by the following code:
Save the file and take a look to your theme: It looks great! We now have a list of posts on our homepage. Once again, we have a very good looking list, with some nice transition effects, without coding a single line of JavaScript. That’s the magic of jQuery mobile.
The list is created by using the data-role="listview" attribute on the unordered list. The other attributes specifies the appearance of the list. Want to experiment a bit? Simply replace data-theme="c" by data-theme="b" and see what happens.
Now, modify the loops of the search.php and archive.php files like we did with index.php.
Step 4: Post and pages filesBy default, the single.php and page.php files from the Blank Theme looks good in our mobile version, so we do not need to modify those files. Though, we can do something to enhance user experience: Mask the comments by default, and show them only if the user decides to. This can be done extremely easily using jQuery mobile.
Open comments.php and locate line 15. Insert the following line:
Then, go to line 31 and insert a closing
Did you noticed that on posts, the header bar is showing a “Back” button? A click/tap on it will take you back to your blog homepage.
Step 5: Implementing searchRight now, we have a theme fully optimized for mobile devices. But it is missing something important: An easy to access search bar. Open your searchform.php file and replace its content by the following:
After you saved the file, reopen index.php and include the search form between the get_header() function and the unordered list which contain our posts:
Our homepage now has a working search form. Right now, our theme is done and we can use it on a production site.
Step 6: Final touchesOf course, even if the theme we’ve built is perfectly functional, there are still a lot of things that can be done to enhance its look and functionality. For example, I have noticed that the search field is smaller than the list items. In order to make the search field as wide as the list items, paste the following in style.css:
.ui-input-search{ width:96% !important;}That’s all for today. I hope you enjoyed this tutorial. If you want to download the finished theme, just click
Also, I just redesigned my other blog







0 comments:
Post a Comment