Pressing Words

Could it be, an actual web-programming related post? Indeed it is, behold…

I made a very silly decision a few weeks ago when I suggested to my workmates ‘yeah, I think Wordpress could be a viable CMS with a bit of hacking’. After weeks of torturous problem solving activities are Javascript hacking, the 8 sites we’ve built so far with it are coming to fruition, and I thought I’d share some of the techniques we’ve developed.

Firstly, one of the main problems we had was changing the layout on different pages slightly to accomodate specific features. At first it was simply a matter of using $_GET[”id”] since the page or post id would be in the URI, but once we started rewriting the permalink structure for SEO purposes, i had to look elsewhere. One of these is the every useful $post->ID, which can be called anywhere on the template, as long as the page represents a single Wordpress ‘Post’ or ‘Page’. The other was discovered when we wanted to have the blog posts show up on a specific ‘Page’, and thats when we discovered that you can implement ‘Page Templates‘. Instead of having the usual loop for the page content, instead I created a whole new wp-query item to loop through the blog posts instead, ignoring whatever content might have been on that page.

I appreciate that this is not exactly new, and yes, I did find most of this on the internet, but it was a rather fundemental discovery for us, which means we can shift these little monkeys quicker to get onto the bespoke programming jobs, seeing as I am effectively lead programmer now (even though I’ve been doing mostly design work for 3 weeks).

The second technique utilizes the post meta. These are those ‘Custom Fields’ you can assign at the bottom of the post and page editting screen in Wordpress. Last time I looked into these on my old theme, I assumed I could only retrieve them whilst within The Loop. However, once I discovered that $post->ID tidbit, it all came a bit simpler.

The problem that need solving was for a shipping site: they wanted a separate flash banner on each page which would loop through a set of 6 images in a folder, which each folder being assigned to one post. Our idea was to assign the folder name for each post in a ‘Custom Field’ called banner. It was all fine and dandy using the get_page_meta function, which will return the value of one specific field, but I realized that it would be great for use all over the template if I could get the meta as an array in PHP. The function get_post_custom does technically achieve this, except the value of each field is an array, regardless of the number of values assigned to that field. For example, if I wanted to get the single value of the banner field, it would be:

To me, and most back-end programmers, there’s not much wrong with that, but I wanted it to be easier for the front-end programmer i.e. get rid of that blasted [0]. So I made myself a little function, return_post_meta:

And yes, this post was also a bit fat excuse to try out my new code highlighter plugin!

So there’s my little contribution, a little snippet of knowledge. If you want to implement this function at all, it belongs in wp-includes/post.php, and let me know by leaving a comment below.

« Courage, the Cowardly Dog Rugby World Cup! »

Comments

Post a comment

RSS Icon RSS Feed | hCard Icon Download vCard | CV Icon Download CV | Going up?
© Luke Williams / Red Root 2008
 

It seems you're using an unsafe, out-of-date browser. Click here to upgrade to Firefox for free. X