Add custom fields with the WPAlchemy MetaBox PHP Class

Using custom fields in WordPress is useful, but the built-in custom field UI leaves much to be desired. A helper class to the rescue.

There are a ton of custom meta box plugins. This is not a plugin, but a PHP class that gives you full control over how things are presented. Download it and get to know it here.

When adding something to a page through a custom field, it is useful to have an if and else in case the field is empty. For a quick refresher on the PHP conditional itself, see the guide to if, else, and elseif.

$custom_metabox->the_meta();
$custom_metabox->the_field('_material');
$my_custom_value = $custom_metabox->get_the_value();
 
// If the $my_custom_value is not empty
if (!empty($my_custom_value)) {
  echo $custom_metabox->get_the_value();
} else {
  // If it is empty
  echo '<span class="un-highlight">--</span>';
}