为这个元框制作表格? (WordPress的)

时间:2012-08-20 01:14:16

标签: wordpress function wordpress-theming

我一直试图在我的模板中将这个元框信息显示为一个表格,但它没有用完。

我想做的是制作一张简单的表格。我所需要的就是帮助我开始的人。元框的代码已完成,但我不知道如何输出它。

$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 . 'anname',
            'type' => 'text',
            'std' => ''
        ),
        array(
            'name' => 'Genre',
            'desc' => 'Is it a thriller, action/adventure, etc...',
            'id' => $prefix . 'angenre',
            'type' => 'text',
            'std' => ''
        ),
        array(
            'name' => 'Directed by',
            'desc' => 'Name of director(s).',
            'id' => $prefix . 'an_director',
            'type' => 'text',
            'std' => ''
        ),
        array(
            'name' => 'Music by',
            'desc' => 'Name of composer(s)',
            'id' => $prefix . 'anmusic',
            'type' => 'text',
            'std' => ''
        ),
        array(
            'name' => 'Studio',
            'desc' => 'Studio which owns the anime.',
            'id' => $prefix . 'anstudio',
            'type' => 'text',
            'std' => ''
        ),
         array(
            'name' => 'Licensed by',
            'desc' => 'Name of both American and Japanese license holders.',
            'id' => $prefix . 'anlicense',
            'type' => 'text',
            'std' => ''
        ),
        array(
            'name' => 'Network(s)',
            'desc' => 'Networks which air the show in both Japan and the United States.',
            'id' => $prefix . 'annetwork',
            'type' => 'text',
            'std' => ''
        ),
         array(
            'name' => 'Original run',
            'desc' => 'Date of when the anime first aired and when it stopped.',
            'id' => $prefix . 'anrun',
            'type' => 'text',
            'std' => ''
        ),
        array(
            'name' => 'Episodes',
            'desc' => 'Number of episodes.',
            'id' => $prefix . 'anepisodes',
            '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);
        }
    }
}

我尝试使用

<table>
<tr>
<td><?php echo get_post_meta($post->ID, "anname", true); ?></td>
</tr>
</table>

作为一种测试一个字段的方法,但它没有奏效。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我相信您使用的代码应该是正确的,但是,您检索的元数据ID似乎缺少前缀变量:“anime_”。尝试:

<?php echo get_post_meta($post->ID, "anime_anname", true); ?>