Archive for the ‘WordPress’ Category

Creating review posts

Friday, November 24th, 2006

I have a feature in this blog that I’ve never used. It’s a special “Review” category. It is styled a little differently than this default post style, and in this article/tutorial, I’m gonna take you through creating this.

First of all, let’s think about reviews. Reviews require certain extra elements, not needed in normal posts. These elements, like ratings, and a cover picture are stuff that you don’t need in normal posts. So, my recommendation would be to first code your loop without any of these extra stuff. However, you should have a clue as to where these extra elements will go in, so that later, when we add the stuff, it’ll be easy to find the area and just add the snippets of code.

You have to update 2 files for this to take the full effect, index.php and single.php
Moron that I am, I completely forgot to tell that while you can place these bits of codes anywhere, they _must_ be placed within the loop to function.

First off, you’ll need a plugin to get this to work. Go and download the plugin “Get custom Field values” from Scott Reilly’s coffeecode site, a really great resource for great WP plugins.

After installing the plugin, it’s time to implement it.

Before that, you should know where the picture uploader uploads its pictures in WordPress, cause we’re going to use it to upload the album covers or what-nots.

Make sure that you upload a single file type only. Otherwise, it won’t pick them up. I’m using gifs.
After uploading the album cover of any size, it’s time to play with custom fields. Write a new custom field with the key ‘postimage’ and the value as the name of the image file, without the extension. For example, if you uploaded james_blunt.gif, the value would be just ‘james_blunt’, albeit without the quotes.

Now, insert this piece of code where you want the album cover to be displayed.

And you'll see the album cover image displayed. Easy as butter!

Now, for ratings. Here, I had already uploaded the madatory _star graphics_ required for ratings. I made 10 graphics, although there is no restriction to how many you can use. Hell, use a 100 star system!! Ok, so, I named the files 1.gif, 1.5.gif, 2.gif, and so on and so forth. Easy to remember, easy to use.

To add the stars, create another custom value with the key 'rate' and value as the file name. Again, the value will be 1, 1.5, 2, and so on and so forth. Finally, at the right spot, add the following code:

And there you go. Your own review system, with no hassles!!
Now, as you most certainly won't want other non-review posts to display the empty img tags, here’s how you remove them.

Say you have a Review category with id 9, where all your reviews are stored. Cleaning up the tags when not using them is easy enough. Just use the following code, and you’re all set:

This uses a simple if statement to check whether a particular post is in the given category (in our case, 9) or not. Then, it serves up the corresponding code.

That's it for this simple tutorial. I hope this is useful to you.

And before I go, I have taken this screenshot of how a review looks at Troidus.

troidus

Brown WordPress theme

Sunday, July 30th, 2006

Well, I’ve finally decided to do something I never thought I’d do. Make themes…

Brown, a simple, elegent theme for WordPress 2, is my first WordPress theme. Go over and download it, give it a whirl, and suggest features for future editions here. It’s 2 columned, hopefully easy to edit, and validates.

Any criticism is welcome.

Per author avatar in WordPress

Sunday, June 11th, 2006

There are many blogs in “the wild” which are run by more than 1 person. Multiple authors contributing to a blog. Many of these blogs also run WordPress.
Now, most multiple user blogs in WordPress have a small writing somewhere below the post which informs the readers who wrote it. But sometimes, that ain’t enough. Large publications often use a per author mugshot to distinguish between authors.
There is an easy way of achieving this in WordPress.

First and foremost, upload the images, with a common extension, and the same name as the author’s first name, to a suitable location, say wp-content/images.

So, for users Bert and Ernie, I should have images Bert.png and Ernie.png. This is essentially important; so double check the names.

Now, within the loop, place the following code anywhere you fancy:

And your lovely mugshot will be out for the World to see!
A simple hack, yet something people rarely use or find.

Simple WordPress different post style hack

Monday, March 20th, 2006

Many of us want to display the first post in our blog a little differently from the rest, as was a request from one of my clients recently. So, after much badgering the good folks at the WordPress IRC channel, I was able to make a simple hack for the aforementioned purpose.
This is so simple, you’ll probably end up saying Do’h!

First of all, here’s the code for you:

Now, let's take it apart.

First, we introduce a variable $i, and give it a value of 1. This has to be done before we begin the WP loop though. Immediately after the loop, we introduce an if statement which checks if the value of $i is 1, or, in layman's terms, whether this is the first time the loop is running. If it is, it will execute all the goodness that is contained within the enclosing curly braces.
After the first post is done, comes the else statement. The else statement simply states that if the value of $i is more than 1, or rather, if this is not the 1st time the loop is looping, execute the other goodness. We then simply close the brace for the else.
Now comes an important part. We now simply increment the value of $i so that with each loop the value of $i increases by 1.

And that's all there is to it. The example I've shown you is really a very simple example, and you are limited only by your imagination. For example, you may want the 5th post to have a black background. You simply change the if statement, and you're on your way.

So, I hope this is of some use to someone out there. Enjoy!

WordPress and the annoying rewrite rules

Thursday, March 16th, 2006

My new design, just off the corner, relies heavily on some severe mod_rewriting. So, you can imagine my surprise, when, after upgradin to WP 2.0, I found a .htaccess file that wouldn’t take custom rewrites, thanks to WordPress’ internal rewriting.

After trawling through the forums, I found it littered with requests for custom mod_rewriting rules, yet, very little info. It seems that WordPress generates the following rewrite rule when editing .htaccess and adds values via it’s internal files and the database!

As you can see, it's not possible to simply edit this file. As of writing this post, I still haven't been able to edit the rules. If anyone knows how to do it, kindly, do help.

I will post an update if and when I get a hack/solution.

Update: Matt Read has provided an answer to the problem! Long live Matt! Edit classes.php inside the wp-includes directory and set set var $use_verbose_rules = false; to true, and update your permalink structure. Your .htaccess file should now contain all the several lines of code originally supposed to be there.