Thord Daniel Hedengren - Smashing WordPress_ Beyond the Blog-Wiley (2014)

(avery) #1

142 PART II • Designing and Developing WordPress Themes


Suppose you have a custom field with a key called Actor with the value Harrison Ford.
Then you have another custom field called Director with the value Steven Spielberg.
Finally, you have a custom field called Movie Title with the value Indiana Jones and
the Raiders of the Lost Ark. All these custom fields and their values belong to a
specific post, and if you put the template tag the_meta() somewhere within the loop, and
this post shows, it will output this code:

<ul class='post-meta'>
<li>
<span class='post-meta-key'>Actor</span> Harrison Ford
</li>
<li>
<span class='post-meta-key'>Director</span> Steven Spielberg
</li>
<li>
<span class='post-meta-key'>Movie Title</span> Indiana Jones and the
Raiders of the Lost Ark
</li>
</ul>

CREATING META BOXES
One way to make it a bit easier to work with metadata is to bypass the custom field’s interface
altogether and create your own meta boxes. This is done with add_meta_box(). Go ahead
and create a meta box for your theme, featuring a simple input field and some explanatory
text.

You’ll create the meta box in a function, smashing_add_meta_boxes(), which in turn
will be hooked to load-post.php and load-post-new.php:

// Hook on to add_meta_boxes
add_action( 'add_meta_boxes', 'smashing_add_meta_boxes' );

Next, you need to create the smashing_add_meta_boxes() function as well:

// Create meta boxes
function smashing_add_meta_boxes() {
// Creating our demo box
add_meta_box(
'smashing-post-demo',
'Example Box',
'smashing_post_demo_meta_box',
'post',
'side',
'default'
);
}
Free download pdf