Custom fields in WordPress are great. It allows you to add your own user defined fields to your blog posting. I.e. putting in variables that you can use to further categorise your team.
I use custom field in my team to do two things:
- To display the icon on the index page
- To define images that will be put on my revolving slideshow on top left side
The theme which is use is done by somebody somewhere – and I have to further customise to suit my needs. The problem that i face, is that not all wordpress blogging tool support custom field. Therefore, some of my postings will have blank thumbnails on the index page. This is kinda annoying. Recently (or rather yesterday), I added a new plug-in called Twitter Tools. It allows me to put in Daily Digest of all my tweet as a blog posting on my site. The problem with this tool is that, it does not default the custom field. Thus posting by the tool will have missing image on my screen. Gosh…
So what can i do? Well, there’s few solution…
- Write a script to default the thumbnail to be used – Too messy. I am not that good in PHP
- Write a stored procedure or trigger on mysql to automatically add the custom field – Too messy and complicated
- Put a simple if-statement in the theme (index.php) – Easy….
So i decided to put an if-statement – which is option 3. Here is the code..
[php]
<!—modification by fazli to implement default custom field –>
<?php if (strpos($post->post_title,"Twitter Updates") === false) { ?>
<?php if( get_post_meta($post->ID, "Image", true) !== "") { ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Read the rest of <?php the_title_attribute(); ?>"><img src="<?php echo get_post_meta($post->ID, "Image", true);?>" /></a>
<?php } ?>
<?php } else { ?>
<!–default to twitter thumbs–>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Read the rest of <?php the_title_attribute(); ?>"><img src="http://sharing.mohamadfazli.com/twitter_thumb.jpg" /></a>
<?php } ?>
[/php]
What it does really, is as shown in the diagram below…
So – it solves my problem of empty Image custom field, as well as defaulting my twitter updates to the twitter thumbnail. Fantastic!
Following are some of the information that will be helpful:
- $post – information on the post
- get_post_meta – get the custom fields
- strpos – get the substring of a string in PHP
Hope this somewhat helps you in using thumbnails