0

I'm trying to get my Meta Box to display at the bottom of my post excerpt on the blog index. I've been working on the site for the last 10 hours so I think I'm being blind or have I done it wrong? Would someone please take a look and tell me what I'm doing wrong?

Here is my meta box data:

 function true_add_a_metabox() {
    add_meta_box(
        'true_metabox', // metabox ID, it also will be it id HTML attribute
        'Forum Discussion URL', // title
        'true_display_metabox', // this is a callback functions, which will be print HTML of our metabox
        'post', // post type
        'normal', // position of the screen where metabox shoul be displayed (normal, side, advanced)
        'high' // priority over another metaboxes on this page (default, low, high, core)
    );
}


    add_action( 'admin_menu', 'true_add_a_metabox' );

    function true_display_metabox($post) {
    /*
     * needs for security checks
     */
    wp_nonce_field( basename( __FILE__ ), 'true_metabox_nonce' );
    /*
     * lets add a simple textarea field
     */
    $html .= '<p><label>Forum URL <input type="text" name="forumurl" value="' . get_post_meta($post->ID, 'true_title',true) . '" /></label></p>';

    /*
     * print all of this
     */
    echo $html;
}

    function true_save_post_meta( $post_id, $post ) {
    /* 
     * Security checks
     */
    if ( !isset( $_POST['true_metabox_nonce'] ) || !wp_verify_nonce( $_POST['true_metabox_nonce'], basename( __FILE__ ) ) )
        return $post_id;
    /* 
     * Check current user permissions
     */
    $post_type = get_post_type_object( $post->post_type );
    if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
        return $post_id;
    /*
     * Check if the autosave
     */
    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
        return $post_id;

    if ($post->post_type == 'post') { // define your own post type here
        update_post_meta($post_id, 'true_title', esc_attr($_POST['forumurl']));
    }
    return $post_id;
}

add_action( 'save_post', 'true_save_post_meta', 10, 2 );

And I'm using the following to display the data entered in the field in my template:

<a href="<?php echo get_post_meta( $post->ID, 'true_metabox', true ); ?>">Discuss...</a>
0

Try chaining the action hook for first function from:

add_action( 'admin_menu', 'true_add_a_metabox' );

to below:

add_action( 'add_meta_boxes', 'true_add_a_metabox' );

Also make sure that 'Forum Discussion URL' under 'Screen options' is checked to ensure it displays on edit post screen :)

  • Hi Thanks for the answer, Unfortunately that hasn't worked. The actual meta box is showing in the post screen, its also saving the data that's entered. However when i use the html to display the data, its not showing . – Randomer11 Oct 21 '14 at 13:35
  • You need to fetch the value using meta key and not meta-box id. Use below code and it shall work. If not then you need to confirm the context where you are using $post->ID to ensure $post is available to get the post ID <pre> <a href="<?php echo get_post_meta( $post->ID, 'true_title', true ); ?>">Discuss...</a> </pre> – Prasad Nevase Oct 21 '14 at 17:58
0

@Randomer11 - I was not able apply formatting to my code in above comment, hence posting it here again:

You need to fetch the value using meta key and not meta-box id. Use below code and it shall work. If not then you need to confirm the context where you are using $post->ID to ensure $post is available to get the post ID

<a href="<?php echo get_post_meta( $post->ID, 'true_title', true ); ?>">Discuss...</a>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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