Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

<?php

$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">

<?php
    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>
</div>

<?php } ?>

</div>
<?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
add comment

1 Answer

up vote 1 down vote accepted

Try this?

<?php        
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
show 1 more comment

Your Answer

 
discard

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.