OK folks, here’s the final installment of the hefty ‘create your own wordpress theme’ tuts. What I’m basically going to do to this time is demonstrate a few different small php scripts that bring that ever-important wordpress functionality to your site. I will outline where the different snippets go within your code, and what each bit means, so you can play with the variables yourself!
The fantastic thing about WordPress is; there is so much you can do with it, it’s so versatile and there’s a massive community out there constantly coming up with new plugins, ideas and improvements. To discover more that you can do with this awesome blogging platform – visit the WordPress site at www.wordpress.org or go to the wordpress codex section for more code and help.
Blog functionality
OK, let’s attack the bulky bit first. WordPress’ central use is as a blog. The wordpress backend already gives you all the tools you need to create, edit and organise your blog articles – but you need to put some code into your theme’s files so the blog articles appear on the site.
Treating the index.php file as your primary blog page, the code below will input blog functionality into your site. You will most likely want to place this information between the ‘content’ div, where we currently have just a paragraph of text. The descriptions in brackets are just to tell you what each line’s purpose is, don’t actually include this in your code:
<?php if(have_posts()) : ?> (php asks if there are any posts)
<?php while(have_posts()) : the_post(); ?> (if there are posts – do this below:)<div class=”post” id=”post-<?php the_ID(); ?>”> (begins div ‘post’ – the php automatically assigns the wordpress post id as the CSS id – so you can edit individual posts using CSS)
<h2><a href=”<?php the_permalink(); ?>” rel=”bookmark” title=”<?php the_title(); ?>”><?php the_title(); ?></a></h2> (creates the post header – the php draws in the individual blog titles from the database.)
<div class=”entry”> (starts div ‘entry’)
<?php the_content(); ?> (php brings in the content from your article)
<p class=”postinfo”> (starts paragraph ‘postinfo’)
<?php _e(‘Filed under:’); ?> <?php the_category(‘, ‘) ?> <?php _e(‘by’); ?> <?php the_author(); ?> (here the php calls up loads of meta information about the articles and cleverly inserts it to create a nice phrase – ‘Filed under ‘category’ by ‘author name’)
<br />
<?php comments_popup_link(‘No Comments »’, ’1 Comment »’, ‘% Comments »’); ?> (php displays if there are any comments and provides a link to these comments)
<?php edit_post_link(‘Edit’, ‘ | ‘, ”); ?> (inserts link for post author to edit post)
</p> (closes paragraph ‘postinfo’)<!– <?php trackback_rdf(); ?> –> (pulls in RSS capabilities)
</div> (ends div ‘entry’)
</div> (ends div ‘post’)<?php endwhile; ?> (end php condition ‘if there are posts’)
<?php else : ?> (if there aren’t any posts – do this instead:)
<div class=”post”>
<h2><?php _e(’404 Error: Not Found’); ?></h2> (displays 404 error page)
</div>
<?php endif; ?> (ends php whatif statement)
There seems a lot of code there but if you take out my descriptions and condense it, it’s only short and pulls in plenty of information on your posts. You only need to put this code in once, regardless of how many posts you have it will pull all the posts up and display them in order on your page
If you want to display the date on your blog posts you can insert the following php where you wish it do display, in the ‘postinfo’ area’s usually your best bet:
<p>Posted: <?php the_time(‘F j, Y’); ?> at <?php the_time(‘g:i a’); ?></p>
which will render in the format:
‘Posted: May 9th, 2008 at 10:28am’
For information on inserting date and time information into your wordpress themes – visit the WordPress docs section here.
There is a plethora of other mini-scripts and lines of code you can slap into the body of your theme to spruce it with functionality but above outlines the major blog technology that made WordPress so popular in the first place.
Now a few other bits:
Header
There’s little bits of WP php we can even sneak into your header.php file to add tricks to your site. The first few below are to be placed within the <head> area:
Title
<title><?php bloginfo(‘name’); ?><?php wp_title(); ?></title> (replaces your static title with a dynamic title that changes according to each individual page name as specified in your WordPress posts.)WordPress Stats
<meta name=”generator” content=”WordPress <?php bloginfo(‘version’); ?>” /> (this doesn’t really do anything for your blog but helps WP to gather stats on it’s users)RSS/Atom Feeds
<link rel=”alternate” type=”application/rss+xml” title=”RSS 2.0″ href=”<?php bloginfo(‘rss2_url’); ?>” /> (RSS2)
<link rel=”alternate” type=”text/xml” title=”RSS .92″ href=”<?php bloginfo(‘rss_url’); ?>” /> (RSS 1)
<link rel=”alternate” type=”application/atom+xml” title=”Atom 0.3″ href=”<?php bloginfo(‘atom_url’); ?>” /> (Atom)
<link rel=”pingback” href=”<?php bloginfo(‘pingback_url’); ?>” /> (pingback)Archives
<?php wp_get_archives(‘type=monthly&format=link’); ?> (displays links to your monthly archives)
There are also other snippets of code that can actually go anywhere in your site but are most likely found in the header area, but are no good within <head> and must make sure they’re placed within the actual <body> of your site:
Header Title
<h1><a href=”<?php bloginfo(‘url’); ?>” title=”<?php bloginfo(‘name’); ?> <?php _e(‘home page’); ?>”><?php bloginfo(‘name’); ?></a></h1> (creates a header link with your blog name, as specified in the WordPress options area – the name you decided when building your WP site.)Header Description
<p><?php bloginfo(‘description’); ?></p> (pulls in the description, usually to be used as a slogan or tagline, which can also be changed in the options section of WordPress)
Menu and Sidebar
If you want to pull your pages into a menu automatically, so you don’t have to code new links into your menu everytime you create a new page, or if you want your categories, archives and such displayed in your menu/sidebar – the code below is for you:
Insert Pages
<ul> <?php wp_list_pages(‘depth=1&title_li=’); ?> </ul> (pulls your individual page links into an unordered list)Display Categories
<ul> <?php wp_list_cats(‘sort_column=name&optioncount=1&hierarchical=0′); ?> </ul>Display Archives
<ul> <?php wp_get_archives(‘type=monthly’); ?> </ul>Display Blogroll
<?php get_links_list(); ?>Meta
<ul>
<?php wp_register(); ?> (new users click here to register)
<li> <?php wp_loginout(); ?> </li> (login/logout)
<li> <a href=”<?php bloginfo(‘rss2_url’); ?>” title=”<?php _e(‘Syndicate this site using RSS’); ?>” class=”feed”><?php _e(‘Entries <abbr title=”Really Simple Syndication”>RSS</abbr>’); ?></a> </li> (sign up to post RSS feeds)
<li> <a href=”<?php bloginfo(‘comments_rss2_url’); ?>” title=”<?php _e(‘Syndicate comments using RSS’); ?>”><?php _e(‘Comments <abbr title=”Really Simple Syndication”>RSS</abbr>’); ?></a> </li> (sign up to comment RSS feeds)
<li> <a href=”http://validator.w3.org/check/referer” title=”This page validates as XHTML 1.0 Transitional”><?php _e(‘Valid’); ?> <abbr title=”eXtensible HyperText Markup Language”>XHTML</abbr></a> </li> (is this site valid?)
</ul>
Footer
I’m not really going to bother with the footer, as most people tend to put their own links in the area. Plus there’s not really that much that can go down there – you could pull in your page links as above, or put a link to WordPress.
The only script that pops to mind is the ‘timer’ which shows how long it takes for the page to be displayed:
<!– <?php echo get_num_queries(); ?> queries. <?php timer_stop(1); ?> seconds. –>
But to be honest that’s pretty out-of-date now.
Other Little Bits
There are literally hundreds of scripts and plugins for WordPress, constantly being developed all the time. But there are a certain few that just never die and always pop up on blogs, either because they’re so useful or just because people think they actually serve some purpose.
Search
<form method=”get” id=”searchform” action=”<?php bloginfo(‘home’); ?>/”>
<div>
<input type=”text” value=”<?php echo wp_specialchars($s, 1); ?>” name=”s” id=”s” size=”15″ /><br />
<input type=”submit” id=”searchsubmit” value=”Search” />
</div>
</form>(the above form should search your blog for given keywords, but I would recommend taking a peek at other themes to create a ‘search results’ page and to actually hide the above code away in it’s own file that can easily be called up anywhere on your blog with the following php command:)
<?php include (TEMPLATEPATH . ‘/searchform.php’); ?> (/searchform.php refers to the filename of your search script)
The Dreaded WordPress Calendar
<?php get_calendar(); ?> (usually displayed in the sidebar – make sure to place it within <li> if it’s in a menu.)
Widgets
If you’re not sure what Widgets are, check them out here. They’re basically another method by the great WP community to enhance your blog and make adding and editing functionality to it far easier and cleaner.
To insert widgets into your site anywhere, 2 lines of code are all you need:
<?php if ( function_exists(‘dynamic_sidebar’) && dynamic_sidebar() ) : else : ?>
(in here insert any content you want to display in case you don’t have widgets)
<?php endif; ?>
You can then use the ‘Widgets’ tab in the Presentation area of WordPress to add and play with your widgets! (that sounds rude… not intended!)
However, there is also a Widgets plugin you can get hold of from the WordPress site and most likely many other places on the web. I haven’t actually used this plugin so I’m not quite sure what’s required and what the benefits are. Why not check it out for yourselves?
That’s all for today folks
I keep mentioning this at the end of every WP tut. WordPress is easy to use, it’s exceptionally well built but has such extreme potential if you wish to push it, experiment and learn. These tutorials don’t even scratch the surface, but provide a slight insight into how you go about building your own theme – which, especially if you’re a designer/developer offering your own services built on the WordPress package – it’s pretty essential to know how to do.
The best way to get really handy with anything, not just WordPress, is to keep trying different things. Take an existing theme – break it down and find out what everything does and why it does it. Then try incorporating new scripts, designs, plugins etc and see what happens! You will never fully explore WP capabilities – it’s just too versatile!
I hope this series has provided useful. You can find part1 here and part 2 here. If you have any questions or problems please leave comments or get in touch via my contact page.
Nathan
gaaash. this is the most informative yet simple tutorial i have read. thanks a lot! i’ll start making my own layout now.
Cook article!
May I use it for my site?
Cool article!
May I translate it to Russian and post it on my site?
Of course, link to you will be added.
Of course you can Vladimir, thanks for asking.
im just confused about the slices for images, can u pls make a clear details about it? or how many images need for the template…
those blog functionality codes where will i put them? coz there is already some php codes in the index.php header.php etc. sorry for being noob im just interested to learn how to make a customized wp theme…
John – if you want to contact me directly and I can run through it with you properly.
i need a clearly detail about the images. thank you
Great explication of the code inside a blog. You make it very easy to comprehend. I’m trying to style my wordpress blog now, and look forward to using your tutorial here to get it done.
Wow, what a great post! Very informative and easy to follow! I’ll keep this one bookmarked!
Great post on programming functionality to WordPress themes. I won’t be creating a theme soon, but your article will hopefully help me understand the bulk of my theme files to make life easier for me when I’m tweaking my theme.
Although I could create a theme and could add some functionality but your post clear a lot of mess in my mind. I was creating themes by editing one of existing theme in my computer but now I know how to do it actually.
Thank you for this information.
Great! I’m in on this too. Man this was rreeeally helpful.
So.. how do yo do your comments? you did those by hand and with gravatars? or is it plugin?
thanks
There are thousand posts out there on the wordpress themes but it is really good to read a detailed post on it. I think all developers out there publish posts just for traffic but you did a good job on details.
I like your default gravatar because I haven’t seen such icon anywhere else. I think we must handle a wordpress blog in a sophisticated way. I can see thousands blogs over internet with a lot of creepy stuff. Your blog is very clean and simple. Thanks for sharing your article on the wordpress themes. I think you could explain it in more details for newbies.
Wow wow wow. That’s awesome. Really cool tutorial. Simple but effective. I like that. Good job. I’m still new to blogging and this will help me a lot. Thanks again. Cheers
This is great. It’s very overwhelming to look at all the code, so thank this. Very beneficial!
wasn’t there a new release for this?
Thanks alot for this great tutorial. I was looking for a tutorial of how i can tweak my own theme
I just wanted to leave a quick comment to thank you for actually taking the time to comment and show what you were doing in your code.
It is so rare for theme developers to share the “why” information like that. Thanks!
I needed something unique for my new website which must be clean and elegant. I changed edit wordpress files and I have a nice and clean blog. I needed to have matrix style posts of content so I changed sidebar and now it has posts in that as well. You can check it at www(dot)msofficeworld(dot)net
Feel free to tell me that if you like the design or not.
Great. Simply awesome tutorial. Everything I was looking for the past whole week is just here in one post
Thanks a lot for your effort. I’m currently making a WordPress them for my own blog and I really needed some coding help. Thanks again
mm… nice
good one.
Sweet! Thanks a bunch for taking the time to write such an informative article. Keep ‘em coming, please!
May I suggest you use actual comment tags in front and after you comments?
To edit themes, we must know how we can build them so all your posts regarding this are wonderful to read and is a big help. I now can edit themes in more sensible way.
I wanted to create a few pages for my blog and all your tutorial series is wonderful to teach someone wordpress. I am feeling pretty well right now to do that task myself.
I’m always messing around with WP, trying to customise the various templates to what I really want and this helps a great deal, not just for me but also for teaching others – thanks muchly for this very useful post.
Thanks a lot, the scrips works great, and help to expand my owns, my partner was thinking moving to drupal, i dont like it, but with your scripts you help to convinced him… wordpress is better.
Wow! I was looking everywhere for this! Thats a bunch. It works great!
Joe
I just finished going through Part2 of your tutorial. A bit slow but I’m learning alot from your articles. Many Thanks…
Thanks for the great informative tutorial. Very helpful to me. Appreciate the effort.
Thank you! I would now go on this blog every day! Also you can learn from this service on this issue. Home-improvement
This is a treasure. Thanks for the tutorials