
Capture and display custom data types using ICE and More Fields
Building on the concept of category templates which I started with the FAQ template tutorial last week, this tutorial will cover how to capture and display custom fields in a template. What’s more, I’ll go through the step-by-step process of how to implement this using More Fields which is a plugin that “enables you to define post types, which are custom Write/Edit pages that contains a pre-defined set of boxes” that store their data behind the scenes in custom fields. The concepts from this tutorial will be of interest for anyone who:
- Has multiple categories of blog content with unique data to capture / present. Thumbnail per post, photo of the day or other customized items.
- Uses WordPress as a CMS for unique page data types (event calendar, classified ads, business partners, etc)
- Finds the selection of custom fields from a drop-down to not be the best user interface experience for capturing structured data.
Direct Video Link:http://www.screencast.com/t/2dcpYsSlk
Example Code
The only real code in this template that is unique is the retrieval of data from a custom field using the get_post_meta function.
A single custom field retrieval
Place this example within the loop of an existing category / post template.
<?php $locationPhone = get_post_meta($post->ID, 'locationPhone', true); if ($locationPhone != '') { ?> <div class="locationInfo"> <h4>Phone Number</h4> <?php echo $locationPhone; ?> </div> <?php } ?>
Full retail location fields
This example is the full loop for use within a category.php file to display the contents of custom fields named: location
<?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <h2><?php echo the_title('','',false); ?></h2> <?php the_content(); ?> <div class="clear"></div> <?php $locationAddress = get_post_meta($post->ID, 'locationAddress', true); if ($locationAddress != '') { ?> <div class="locationInfo"> <h4>Address</h4> <?php $locationMapURL = get_post_meta($post->ID, 'locationMapURL', true); ?> <?php if ($locationMapURL != '') { echo "<a href='" . $locationMapURL . "'>"; ?> <?php } ?> <?php echo $locationAddress; ?> <?php if ($locationMapURL != '') { echo "</a>"; } ?> </div> <?php } ?> <?php $locationPhone = get_post_meta($post->ID, 'locationPhone', true); if ($locationPhone != '') { ?> <div class="locationInfo"> <h4>Phone Number</h4> <?php echo $locationPhone; ?> </div> <?php } ?> <div class="clear"></div> <?php $locationHours = get_post_meta($post->ID, 'locationHours', true); if ($locationHours != '') { ?> <div class="locationInfo"> <h4>Business Hours</h4> <?php echo $locationHours; ?> </div> <?php } ?> <?php $locationWebsite = get_post_meta($post->ID, 'locationWebsite', true); if ($locationWebsite != '') { ?> <div class="locationInfo"> <h4>Website</h4> <?php echo "<a href='" . $locationWebsite . "'>" . $locationWebsite . "</a>"; ?> </div> <?php } ?> <div class="clear"></div> <?php endwhile; else : ?> <p><?php _e('Sorry, no posts matched your criteria.'); ?></p> <?php endif; ?>
2 Comments
This plugin works for post if you have a page tied to a category will it also work for pages..?
While the functions involved would all work with display from a page, you will have to setup a different query loop in order to get the “sub-pages” or whatever elements you are using to store the retail location info.
One Trackback
[...] your dreams into reality together! Skip to content HomeServicesPortfolioProjectsBlogAbout « Template – Retail Locations Solving a custom field query quandry [...]