0

I want to display the information for my custom metabox as a table on my posts. First question is, how do I make a table out of the information, and secondly, how do I call the metabox?

Also, can I have the table disappear if nothing is filled in?

$prefix = 'anime_';

$anime_box = array(
    'id' => 'anime-meta-box',
    'title' => 'Anime Details',
    'page' => 'post',
    'context' => 'normal',
    'priority' => 'high',
    'fields' => array(
        array(
            'name' => 'Name',
            'desc' => 'Add the name of the Anime in either English or Japanese(Romanji).',
            'id' => $prefix . 'anime_name',
            'type' => 'text',
            'std' => ''
        ),
        array(
            'name' => 'Genre',
            'desc' => 'Is it a thriller, action/adventure, etc...',
            'id' => $prefix . 'anime_genre',
            'type' => 'text',
            'std' => ''
        ),
        array(
            'name' => 'Directed by',
            'desc' => 'Name of director(s).',
            'id' => $prefix . 'anime_director',
            'type' => 'text',
            'std' => ''
        ),
        array(
            'name' => 'Music by',
            'desc' => 'Name of composer(s)',
            'id' => $prefix . 'anime_music',
            'type' => 'text',
            'std' => ''
        ),
        array(
            'name' => 'Studio',
            'desc' => 'Studio which owns the anime.',
            'id' => $prefix . 'anime_studio',
            'type' => 'text',
            'std' => ''
        ),
         array(
            'name' => 'Licensed by',
            'desc' => 'Name of both American and Japanese license holders.',
            'id' => $prefix . 'anime_license',
            'type' => 'text',
            'std' => ''
        ),
        array(
            'name' => 'Network(s)',
            'desc' => 'Networks which air the show in both Japan and the United States.',
            'id' => $prefix . 'anime_network',
            'type' => 'text',
            'std' => ''
        ),
         array(
            'name' => 'Original run',
            'desc' => 'Date of when the anime first aired and when it stopped.',
            'id' => $prefix . 'anime_run',
            'type' => 'text',
            'std' => ''
        ),
        array(
            'name' => 'Episodes',
            'desc' => 'Number of episodes.',
            'id' => $prefix . 'anime_episodes',
            'type' => 'text',
            'std' => ''
        ),

    )
);

add_action('admin_menu', 'anime_add_box');

// Add meta box
function anime_add_box() {
    global $anime_box;

    add_meta_box($anime_box['id'], $anime_box['title'], 'anime_show_box', $anime_box['page'], $anime_box['context'], $anime_box['priority']);
}

// Callback function to show fields in meta box
function anime_show_box() {
    global $anime_box, $post;

    // Use nonce for verification
    echo '<input type="hidden" name="anime_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';

    echo '<table class="form-table">';

    foreach ($anime_box['fields'] as $field) {
        // get current post meta data
        $meta = get_post_meta($post->ID, $field['id'], true);

        echo '<tr>',
                '<th style="width:20%"><label for="', $field['id'], '"><strong>', $field['name'], ':</strong></label></th>',
                '<td>';
        switch ($field['type']) {
            case 'text':
                echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />',
                    '<br /><small>', $field['desc'],'</small>';
                break;

        }
        echo    '<td>',
            '</tr>';
    }

    echo '</table>';
}

add_action('save_post', 'anime_save_data');

// Save data from meta box
function anime_save_data($post_id) {
    global $anime_box;

    // verify nonce
    if (!wp_verify_nonce($_POST['anime_meta_box_nonce'], basename(__FILE__))) {
        return $post_id;
    }

    // check autosave
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return $post_id;
    }

    // check permissions
    if ('page' == $_POST['post_type']) {
        if (!current_user_can('edit_page', $post_id)) {
            return $post_id;
        }
    } elseif (!current_user_can('edit_post', $post_id)) {
        return $post_id;
    }

    foreach ($anime_box['fields'] as $field) {
        $old = get_post_meta($post_id, $field['id'], true);
        $new = $_POST[$field['id']];

        if ($new && $new != $old) {
            update_post_meta($post_id, $field['id'], $new);
        } elseif ('' == $new && $old) {
            delete_post_meta($post_id, $field['id'], $old);
        }
    }
}
2
  • Any progress on this one?
    – kaiser
    Commented Mar 3, 2013 at 11:35
  • Yes, the answer below was enough. Commented Mar 5, 2013 at 2:50

1 Answer 1

0

Each field in the meta box is being saved as post meta (a.k.a. custom fields). You can display the info per-field with the get_post_meta function, i.e.:

<?php echo get_post_meta( $post->ID, 'anime_anime_genre', true ); ?>

The parameter in the middle (anime_anime_genre) is each field ID specified in your code above. The reason "anime" appears twice is because that's how you've written it: the ID is set to $prefix . 'anime_genre', and your $prefix is set to anime_.

To make the data appear below post content, you're going to have to find the appropriate template file in your theme to edit. Once you do, just make an HTML table...

<table>
    <tr><th>Genre</th></tr>
    <tr><td><?php echo get_post_meta( $post->ID, 'anime_anime_genre', true ); ?></td></tr>
</table>

And finally, to "make the table disappear if nothing is filled in", just use if statements:

<?php if ( get_post_meta( $post->ID, 'anime_anime_genre', true ) ) { ... } ?>

Beyond that, I'd recommend looking up some basic PHP tutorials, as well as checking out the WordPress Codex to learn about theme development and see how things like custom fields work.

2
  • 1
    <tr><th>Genre</th></tr> would lead to a better css
    – Christian
    Commented Aug 26, 2012 at 5:45
  • Nice catch - edited my response. Commented Aug 26, 2012 at 21:07

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.