wordpress-logo-(1).gifOne of my favourite features of WordPress is that it lets you create custom fields for storing information relating to specific posts or pages. This is really useful when using WordPress as your CMS, because it lets you associate specific pieces of information with certain elements of your site, which you can then use when developing your theme template.

One issue that I came across whilst recently working on a WordPress site was that I needed to check the database for the existence of a value for a page’s custom field and assign it a default value if it didn’t exist. The initial code for this worked out to be quite verbose.

Now being the OCD perfectionist that I am, I wanted to do this in as little code as possible, the following is what I came up with. This code snippet will essentially grab the value of the custom field named ‘tb_width’ if it exists and assign it a value of 120 if it doesn’t.

$tb_width = ($tb_width = get_post_meta($post->ID, 'tb_width', true)) ? $tb_width : 120;


Now I realise that it would be better for performance purposes if we did this the long way (probably), but I like that this will only ever need occupy one line of code in your template and sometimes readability and neatness is important.

Obviously use this at your own risk and I welcome any comments/criticisms that aren’t pedalling pharmaceuticals.

You may also like

Still funky or now clunky?

It is not a revelation, but as with most technologies the internet is always evolving. We are always finding ways of improving on the old and with it come a host of enhancements we as internet users have to command and conquer.

Keep Reading

Horizontal navigation menus rule!

We recently performed a website audit for one of our clients. The design we proposed was leaning towards changing the website’s vertical navigation menu to display the product categories horizontally across the page, and it posed the interesting question whether there is still a case using top level left hand vertical navigation menus in modern web design.

Keep Reading

Newsletter sign up

Every couple of months we send out an update on what's been happening around our office and the web. Sign up and see what you think. And of course, we never spam.