September 25th, 2007
Could it be, an actual web-programming related post? Indeed it is, behold…
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:
$meta = get_post_custom($post->ID);
# ^^^ echoes the aforementioned folder name
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 . So I made myself a little function, return_post_meta:
function return_post_meta($post_id = 0)
global $id, $post_meta_cache, $wpdb, $blog_id;
if ( !$post_id )
$post_id = (int) $id;
$post_id = (int) $post_id;
$the_meta = get_post_custom($post_id);
$array = array();
foreach($the_meta as $key => $val)
$array[$key] = $val;
$meta = return_post_meta($post->ID);
echo $meta["banner"]; # now, no 
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.