March 5th, 2012Top StoryHow to Create a Custom Theme for Your WordPress Blog with Minimal Coding RequiredBy Adam Dachis You want to start your own blog but you don't want to look tacky by using an existing design. Creating your own theme can be daunting, but with some assistance you can have a unique design for your blog in no time. This post will help you put it all together using WordPress, the most popular (and free) blogging software available. Creating a WordPress theme can take quite a bit of work when you're starting from scratch because you have to put together quite a bit of PHP code (the programming language WordPress uses) before you can even get to creating the look and feel of your site. We're going to save you the trouble by providing those files pre-made for you, plus a skeletal structure of the CSS you're going to need to make your WordPress site look the way you want. The PHP code is based upon the work of Ian Stewart, but has been updated for WordPress 3.3.1. If you're ready to get started, here's what you'll need:
Once you've got everything, you're ready to start making your theme! Get to Know and Install Your Theme FilesWhen you unzip the lh_wordpress_blank_theme.zip file your downloaded, you'll have a folder filled with a bunch of files and two folders. Here's a quick list of what they are and what they do:
That list probably seems a little intimidating, but chances are you won't need to edit most of those files. We just wanted you to know what everything does. At the very least, you only need to edit styles.css, and maybe footer.php (if you want to add content to your footer). Now that you know what you're in for, let's install this blank theme in WordPress. To do so, just follow these steps:
Alternatively, if you prefer installing via FTP or just moving the files to the correct directory on your local machine, unzip the lh_wordpress_blank_theme.zip and rename it "blank" (or whatever you want). Now find the wp-content folder in your WordPress installation directory and open it. You'll see a directory called "themes" inside. Copy blank into it, then follow the last step in the steps above to activate it on your WordPress site. Style Your WordPress ThemeWith the blank theme installed you're ready to start making your WordPress theme look great. Currently, however, it looks like an ugly mess of blue, green, and black. It may not even look that good if you haven't added a menu, widgets, or any content. If you still need to add content, either download sample content or create some of your own. Even if you install sample content, you'll still need to go into the WordPress Admin, click on the appearance section, and edit both the Menus and Widgets subsections. Just head into the Widgets subsection and (literally) drag some widgets into both the primary and secondary sidebar spaces, then create a menu in the Menus subsection. Once you have all your (possibly fake) content ready, you'll see it all on your WordPress site. It will probably look something like the image to the right. Now that everything is in place, we need to start editing the styles.css file. You can either open it up in your favorite programming-friendly text editor, or you can go to the Appearance tab in the WordPress Admin section, choose Editor, and then choose styles.css from the right-hand menu (if it isn't already selected by default). Either way, you should now see the skeletal CSS structure you're going to use. It does not include every single style you may want to edit, but the main ones you'll need to get most of your style together. (Later on we'll discuss how you can discover other elements and how to style them as well, so if you want to take things further than the template we've provided you'll be able to do so easily.) Since everyone's design is going to vary a little bit, we're going to discuss what most of the styles do rather than offering up specific code you can use. You're going to need to bring your own design and CSS knowledge to the table, so if you're feeling a little shaky you can brush up your skills with our CSS night school lesson. So, without further ado, here's a rundown of the styles.css file. Each item we cover will demonstrate what it looks like now and what it could look like once you add some style. Global: General Style DefaultsThe General Style Defaults are pretty much laid out for you already and should be pretty familiar even if you don't know very much CSS. For the most part these styles are just setting the margins of the document (which are currently at 0 pixels), the default font to use on the page, what colors the background and text are, how links look in their various states, and more. You can leave this information as-is, or go in and make general changes as desired. For the most part, the look and feel of your WordPress site won't be too heavily influenced by these styles and, if anything, the changes you make will mostly relate to the fonts and colors. Page LayoutThe Page Layout section of the styles.css file contains only two code blocks: one for div#container and one for div#content. In both cases we're targeting DIV tags. The one called container is what contains pretty much everything on the page that is not the header or the footer. That means you've got the post, static page, or blog roll, the comments, and the sidebar widgets all wrapped into this one container. By default we've given it a 4px margin to inset all this content just a tiny bit from the header. You can adjust it however you like and add any new styles you wish here. The content DIV, on the other hand, only includes the post, static page, or blog roll (and comments, if they're visible). Any styles you add here will only effect that content. One of the first things you're going to want to do is position this DIV somewhere on the page. Traditionally it is aligned left by using this CSS code: float: left; Chances are you'll also want to define it's width, any margins or padding, and anything else you want to be different in that specific area of your site. Here's what my site's content DIV style looks like: div#content { width: 640px; float: left; margin: 0 0 0 51px; padding: 0 18px 0 0px; } As you can see, it has a width of 640px. It also floats to the left, just like we talked about earlier. Additionally, I've added a 51px margin on the left side and 18px of padding on the right side. How do I know which numbers correspond to which sides? In CSS margins and padding code, it goes like this: top, right, bottom, left. This is easy to remember because, abbreviated, it looks like TRBL. That sounds like trouble or terrible when you sound it out. Either way, that's a quick and easy way to remember it. HeaderThe header is a fun part of your page to style because you get to decide how the menus look. Although you might have additional elements in your header should you decide to add a few new things do the header.php file in your theme, menus are still the main draw. Menu items are given in a list style that, without styling, would look a lot like this:
The dots wouldn't be green, but you'd basically end up with a list. You don't want that, and presumably you want the menu running along the top of the page. We handled that problem for you and styled the menu so it appears as one straight line of options rather than an unordered list. There are two pieces of CSS that make this happen. The first belongs to the .menu class' ul (unordered list) element: .menu ul { list-style: none; margin: 0; } This code takes away the dots and any margin adjustments you'd get with an unordered list by default. The second relevant code block looks like this: .menu li { display: inline; float: left; padding: 0 8px 0 4px; } This code tells the list to display inline, which means it'll display in a line rather than in a traditional list. Additionally, each list item (li) is told to float left (which will give us a little more freedom to move each item around) and have 8px of padding on the right and 4px of padding on the left (so the text isn't so scrunched together. All of that's enough to get you a basic menu, but we also want to style the a element (a as in a href, the link tag) to make all the links in the menu look the way we want. For that, we need this CSS: .menu-item a { font-size: 30px; text-decoration: none; color: #3eaf00; } .menu-item a:hover { color: #5d8a05; } All this code does is set the font size (to 30px), the color of the font to green (specifically #3eaf00), and remove the underline that appears under links by default (text-decoration: none). The hover state just specifies a slightly darker green (#5d8a05) to display when the user rolls over any menu link. This is all pretty straightforward, basic CSS but it allows you to make your menu look different from the other text on your page without resorting to images. If you want to change the text even more, you can consider a different font or converting everything to upper or lowercase text. If you need some good web font options, you can find hundreds in the Google Web Font Directory. Once you've made these style choices you're pretty much done styling the menu and your header. There are definitely other styles you can find and play around with, but we'll discuss how to locate those at the end of the post. Content: Posts and PagesRemember the div#content style we defined earlier? Pretty much everything in this and the following two content sections of this post relate to styles that exist just inside that DIV. Most of the .entry-title .alignright and .alignleft .entry-content .entry-utility .entry-meta .navigation Content: CommentsAlthough the comments section might seem like an easy styling task, it's actually one of the most complex sections of the blog. The comment form, itself, is pretty straightforward, but as people reply to comments and then reply to those replies, you end up with a lot of nested comments. There is also plenty of metadata, as well as an avatar, the shows up alongside a user's comment. This means you've got plenty of elements to style. .comments .comment .children div#respond Content: SidebarStyling the sidebar is actually pretty easy, but it can get more complex if you want to get very specific. WordPress gives you two widget areas: div#primary and div#primary. You can target each of those DIVs individually, or you can add styles to both of them using the .widget-area class. Furthermore, each widget you add to a widget area is added as—surprise!—a list item. The .xoxo class is applied to this list and can be used to separate and style each widget the way you want. If you want to get really specific, however, you'll need to target each widget directly. We'll discuss how to do that in the next section. Additional StylesNow that we've talked about all the basic styles you'll need to target in order to turn this blank theme into your own, let's discuss how you can take it a bit further. WordPress themes contain tons of elements so there are undoubtedly more you'll want to style. In order to easily discover what they are as you're going through the development process, all you need is the right tool. If you're using Chrome, that development tool is built right in. Just preview your theme as you're developing it and open up the Developer Tools. You can do that by going to the View menu and choosing Developer -> Developer Tools. In Firefox (and many other browsers), just download and install Firebug, then click on the Firebug extension to activate it. Whichever tool you use, it'll cause the screen to split into top and bottom halves. The top half will contain the page and the bottom half will contain the pages code. If you mouse over any element on the page you'll see it highlighted in the code so you can inspect further. This will quickly tell you what the element is called and show you any styles already applied to it. You can then use this knowledge to add a new style for that element (or the class applied to it). Just continue doing that until you've found all the styles you want to add to your styles.css file. When you're finished, you'll have a completed theme! If you've made a WordPress theme before and have additional tips for making it easier, please share them in the comments. Additionally, if you use this process to make a WordPress theme yourself, be sure to post a screenshot in the comments as well. Happy theming! |
|
No matter how carefully you plan your goals they will never be more that pipe dreams unless you pursue them with gusto. --- W. Clement Stone
Monday, March 5, 2012
How to Create a Custom Theme for Your WordPress Blog with Minimal Coding Required
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment