WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using the following code to create custom write panels:


$key = "project";
$meta_boxes = array(
    "project_services" => array(
        "name" => "project_services",
        "title" => "Services",
        "description" => "List the services provided for the project."),
    "project_name" => array(
        "name" => "project_name",
        "title" => "Name",
        "description" => "Write the name of the project."),
    "project_overview" => array(
        "name" => "project_overview",
        "title" => "Overview",
        "description" => "Write an overview of the project.")

function create_meta_box() {
    global $key;

    if( function_exists( 'add_meta_box' ) ) {
        add_meta_box( 'new-meta-boxes', ucfirst( $key ) . ' Description', 'display_meta_box', 'post', 'normal', 'high' );

function display_meta_box() {
    global $post, $meta_boxes, $key;

<div class="form-wrap">

    wp_nonce_field( plugin_basename( __FILE__ ), $key . '_wpnonce', false, true );

    foreach($meta_boxes as $meta_box) {
        $data = get_post_meta($post->ID, $key, true);

<div class="form-field form-required">
<label for="<?php echo $meta_box[ 'name' ]; ?>"><?php echo $meta_box[ 'title' ]; ?></label>
<input type="text" name="<?php echo $meta_box[ 'name' ]; ?>" value="<?php echo htmlspecialchars( $data[ $meta_box[ 'name' ] ] ); ?>" />
<p><?php echo $meta_box[ 'description' ]; ?></p>

<?php } ?>


function save_meta_box( $post_id ) {
    global $post, $meta_boxes, $key;

    foreach( $meta_boxes as $meta_box ) {
        $data[ $meta_box[ 'name' ] ] = $_POST[ $meta_box[ 'name' ] ];

    if ( !wp_verify_nonce( $_POST[ $key . '_wpnonce' ], plugin_basename(__FILE__) ) )
        return $post_id;

    if ( !current_user_can( 'edit_post', $post_id ))
        return $post_id;

    update_post_meta( $post_id, $key, $data );

Now, I can call that metadata in the following way:

<?php $data = get_post_meta( $post->ID, 'project', true ); ?>
<p><?php echo $data[ 'project_services' ]; ?></p>

But I would like to call it in this way:

<p><?php project_services(); ?></p>

Any suggestions?

share|improve this question
up vote 1 down vote accepted

Try this?

function get_project_services() {    
         global $post;
         $data = get_post_meta( $post->ID, 'project', true );
         $project_services = $data['project_services'];    
         return $project_services;    

Then in your template file:

<p><?php echo get_project_services(); ?></p>

I think that will do what you're asking?

(Edited to match your comment.)

share|improve this answer
@Chip Bennett thanks but I would just like to display it in this fashion: <p>project_services();</p>, just like you do with the_title(); or the_content(); – janoChen Apr 10 '11 at 19:15
Okay, I updated the example code. See if that works better? :) – Chip Bennett Apr 10 '11 at 19:38
@Chip Bennett For some reason it doesn't work, it only works if I call it directly from the template. – janoChen Apr 10 '11 at 19:51
Are you calling it from inside the Loop? Oh, might need to declare the global $post. Let me update real quick... – Chip Bennett Apr 10 '11 at 21:17
@Chip Bennett Thanks, that worked! – janoChen Apr 11 '11 at 9:14

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.