Make a table out of meta box

时间:2012-08-19 作者:MrMachoman86

我想在帖子中以表格的形式显示自定义元数据库的信息。第一个问题是,如何利用这些信息创建一个表格,第二个问题是,如何调用元数据库?

另外,如果没有填写,我可以让表格消失吗?

$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);
        }
    }
}

1 个回复
最合适的回答,由SO网友:SeventhSteel 整理而成

meta框中的每个字段都将保存为post meta(也称为自定义字段)。您可以使用get_post_meta 功能,即:

<?php echo get_post_meta( $post->ID, \'anime_anime_genre\', true ); ?>
中间的参数(anime_anime_genre) 是上面代码中指定的每个字段ID。“anime”出现两次的原因是因为您是这样写的:ID设置为$prefix . \'anime_genre\', 并且您的$前缀设置为anime_.

要使数据显示在帖子内容下方,您必须在主题中找到适当的模板文件进行编辑。完成后,只需创建一个HTML表。。。

<table>
    <tr><th>Genre</th></tr>
    <tr><td><?php echo get_post_meta( $post->ID, \'anime_anime_genre\', true ); ?></td></tr>
</table>
最后,要“如果什么都没有填写,就让表格消失”,只需使用if语句:

<?php if ( get_post_meta( $post->ID, \'anime_anime_genre\', true ) ) { ... } ?>
除此之外,我建议您查阅一些基本的PHP教程,并查看WordPress Codex以了解主题开发,并了解自定义字段等内容是如何工作的。

结束

相关推荐

Functions.php:从博客中排除类别

所以很明显,如何从模板中排除某些类别,但我不想修改4个模板,使它们忽略某个类别。有没有一种方法可以将某个类别从阅读设置的“博客”集中排除?我正在将博客分配到名为“博客”的页面。。。但显然,档案和搜索也需要对这一超出类别的内容视而不见。我宁愿在里面做functions.php