WordPress Custom Fields and Checking for Values

I have been building sites with WordPress for about a year now and I love it. I love the versatility that WordPress offers and I have had great success with Search Engine Visibility. I just recently read a great article on how to use custom fields to take your wordpress site to the next level.

I setup a few pages on my site StamfordCTGuide, which one section in particular, hotels. I added a few hotels and ran into some issues of missing images. Instead of adding the missing images image to each custom field entry in Admin. I wrote a little php if statement to check to see if there is a value present, and if there isnt – to show a default “no image available”.

Here is the php that I wrote, its simple – but really my first experience with writing php. I have been doing ActionScript 3 for about 6 months, and the basic principles are the same.


			<a href="<?php the_permalink() ?>" rel="bookmark"><img src="<?php
			if ($hotels_img != null) {
			echo $hotels_img[0];
			}
			else if ($hotels_img == null) {
			echo "http://www.stamfordctguide.com/wp-content/themes/stamfordctguide/images/noimage.gif";
			}

			?>" border="0" width="50"></a>

No Responses to “WordPress Custom Fields and Checking for Values”




XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

By submitting a comment here you grant Jake Rutter – Front-End Developer and Designer, CSS, JavaScript and jQuery, PHP, Wordpress, Expression Engine, Magento a perpetual license to reproduce your words and name/web site in attribution. Inappropriate comments will be removed at admin's discretion.