0

Need help displaying my custom meta boxes on a template file.

Following a tutorial

Here's the code from my functions.php:

function get_custom_field( $value ) {
global $post;

$custom_field = get_post_meta( $post->ID, $value, true );
if ( !empty( $custom_field ) )
    return is_array( $custom_field ) ? stripslashes_deep( $custom_field ) : stripslashes( wp_kses_decode_entities( $custom_field ) );

    return false; 
}

// Register the Metabox
function add_custom_meta_box() {
    add_meta_box( 'meta-box', __( 'Candidate Information', 'textdomain' ), 'meta_box_output', 'page', 'normal', 'high' );
}
add_action( 'add_meta_boxes', 'add_custom_meta_box' );


// Output the Metabox
function meta_box_output( $post ) {
    // create a nonce field
    wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' ); ?>

    <p>
        <label for="textfield"><?php _e( 'Riding' ); ?>:</label>
        <input type="text" name="textfield" id="textfield" value="<?php echo get_custom_field( 'textfield' ); ?>" size="50" />
    </p>
<?php
}


// Save the Metabox values
function meta_box_save( $post_id ) {
    // Stop the script when doing autosave
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

    // Verify the nonce. If insn't there, stop the script
    if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;

    // Stop the script if the user does not have edit permissions
    if( !current_user_can( 'edit_post' ) ) return;

    // Save the textfield
    if( isset( $_POST['textfield'] ) )
        update_post_meta( $post_id, 'textfield', esc_attr( $_POST['textfield'] ) );

    }

 add_action( 'save_post', 'meta_box_save' );`

And what I've used to call the meta box from within the loop on my 'content-page.php' file.

<?php get_custom_field('textfield') ?>

I'm new to this — it's my first experience with custom meta boxes — and I'm sure I'm making a simple error.

0

2 Answers 2

-1

Figured it out, in part thanks to The Sufi's comment. I had:

<?php get_post_meta( get_the_ID(), 'textfield', true); ?>

Should have been

<?php echo get_post_meta( get_the_ID(), 'textfield', true); ?>
-2

use get_post_meta( get_the_ID(), 'textfield', true ) inside the loop

2
  • Still no luck. :/ Edited my post above to show more code.
    – SPS
    Jun 2, 2014 at 20:04
  • Please try to provide a more complete answer. This is really not more than a comment.
    – s_ha_dum
    Jun 2, 2014 at 20:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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