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 have created two custom metaboxes using the Meta Box plugin for my cpt called books, and I am confused how to output the values. The values need to be used in sperate places in one of my templates. I tried using get_post_custom() and with get_post_meta() but couldn't get it to work.

Here is my metabox template:

add_action('admin_init', 'rw_register_meta_boxes');
function rw_register_meta_boxes()
{
    $prefix       = 'rw_';
    $meta_boxes   = array();
    // Here is the code to define a meta box
    $meta_boxes[] = array(
        'title' => 'Amazon Affiliate Link',
        'pages' => array(
            'books'
        ),
        'fields' => array(
            array(
                'name' => 'url',
                'id' => $prefix . 'amz',
                'type' => 'text'
            )
        )
    );
    $meta_boxes[] = array(
        'title' => 'Subtitle',
        'pages' => array(
            'books'
        ),
        'fields' => array(
            array(
                'name' => 'Subtitle',
                'id' => $prefix . 'subt',
                'type' => 'text'
            )
        )
    );
    foreach ($meta_boxes as $meta_box) {
        new RW_Meta_Box($meta_box);
    }
}
share|improve this question

1 Answer 1

up vote 1 down vote accepted

I assume you use the plugin 'Meta Box'. It should actually work fine if you try it with this line (Example with the Amazon Affiliate), just substitute $bookID with your ID or get_the_ID():

echo get_post_meta( $bookID, 'rw_amz', true );

Oh, and i just saw that you opened two array when defining your fields, the correct code should be:

add_action( 'admin_init', 'rw_register_meta_boxes' );
function rw_register_meta_boxes() {
    $prefix = 'rw_';
    $meta_boxes = array();
    // Here is the code to define a meta box
    $meta_boxes[] = array(
        'title'    => 'Amazon Affiliate Link',
        'pages'    => array( 'books' ),
        'fields' => array(
            'name' => 'url',
            'id'   => $prefix . 'amz',
            'type' => 'text',
        )
    );
    $meta_boxes[] = array(
        'title'    => 'Subtitle',
        'pages'    => array( 'books' ),
        'fields' => array(
            'name' => 'Subtitle',
            'id'   => $prefix . 'subt',
            'type' => 'text',
        )
    );
    foreach ( $meta_boxes as $meta_box ) {
        new RW_Meta_Box( $meta_box );
    }
}
share|improve this answer
    
Thanks @fischi, this worked perfectly. I edited the question to say that I am using the MEta Box plugin, can't belive I forgot to mention that! –  JPollock Dec 29 '12 at 9:01

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.