是否在字段保留为空时使表格消失?

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

我用我创建的自定义元框中的字段创建了一个表,但现在我想知道如果没有填写任何字段,是否可以让表消失。

这是否太复杂了,是否有更好的解决方案,还是我应该尽可能多地填写所有字段?

这就是我到目前为止所做的:编辑*

    <div>
 <?php
 $anime_anname = get_post_meta($post->ID, \'anime_anname\', true);
 $anime_angenre = get_post_meta($post->ID, \'anime_angenre\', true);
 $anime_andirector = get_post_meta($post->ID, \'anime_andirector\', true);
 $anime_anmusic = get_post_meta($post->ID, \'anime_anmusic\', true);
 $anime_anstudio = get_post_meta($post->ID, \'anime_anstudio\', true);
 $anime_anlicense = get_post_meta($post->ID, \'anime_anlicense\', true);
 $anime_annetwork = get_post_meta($post->ID, \'anime_annetwork\', true);
 $anime_anrun = get_post_meta($post->ID, \'anime_anrun\', true);
 $anime_anepisodes = get_post_meta($post->ID, \'anime_anepisodes\', true);

 if (
    !empty($anime_anname)
    || !empty($anime_angenre)
    || !empty($anime_andirector)
    || !empty($anime_andmusic)
    || !empty($anime_anstudio)
    || !empty($anime_anlicense)
    || !empty($anime_annetork)
    || !empty($anime_anrun)
    || !empty($anime_anepisodes)

) {
// Table goes here.
}
这是元框的代码:

$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 . \'andirector\',
            \'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);
        }
    }
}
最后,主题的创建者做了类似的事情,如果没有输入任何内容,字段就会消失。以下是他所做的:

<div class="post-review">

                    <div class="review-thumb"><?php the_post_thumbnail(\'review-thumb-big\'); ?></div>

                    <div class="review-score">

                        <div class="overall-score <?php $send_rate = get_post_meta($post->ID, "leetpress_overallscore", true); rating_color($send_rate); ?>">
                            <span class="the-score"><?php echo get_post_meta($post->ID, "leetpress_overallscore", true); ?></span>
                            <span class="overall-text">Overall Score</span>
                        </div>
                        <div class="other-score">
                            <?php if(get_post_meta($post->ID, "leetpress_criteria1", true)) { ?>
                            <div class="score-item">
                                <span class="score-label"><?php echo get_post_meta($post->ID, "leetpress_criteria1", true); ?>:</span>
                                <span class="score"><?php echo get_post_meta($post->ID, "leetpress_crit1_rating", true); ?>/10</span>
                                <div class="score-bg" style="background:url(<?php echo get_template_directory_uri(); ?>/images/score-<?php echo get_post_meta($post->ID, "leetpress_crit1_rating", true); ?>.png) no-repeat;"></div>
                            </div>
                            <?php } ?>
                            <?php if(get_post_meta($post->ID, "leetpress_criteria2", true)) { ?>
                            <div class="score-item">
                                <span class="score-label"><?php echo get_post_meta($post->ID, "leetpress_criteria2", true); ?>:</span>
                                <span class="score"><?php echo get_post_meta($post->ID, "leetpress_crit2_rating", true); ?>/10</span>
                                <div class="score-bg" style="background:url(<?php echo get_template_directory_uri(); ?>/images/score-<?php echo get_post_meta($post->ID, "leetpress_crit2_rating", true); ?>.png) no-repeat;"></div>
                            </div>
                            <?php } ?>
                            <?php if(get_post_meta($post->ID, "leetpress_criteria3", true)) { ?>
                            <div class="score-item">
                                <span class="score-label"><?php echo get_post_meta($post->ID, "leetpress_criteria3", true); ?>:</span>
                                <span class="score"><?php echo get_post_meta($post->ID, "leetpress_crit3_rating", true); ?>/10</span>
                                <div class="score-bg" style="background:url(<?php echo get_template_directory_uri(); ?>/images/score-<?php echo get_post_meta($post->ID, "leetpress_crit3_rating", true); ?>.png) no-repeat;"></div>
                            </div>
                            <?php } ?>
                        </div>

                    </div>

                    <?php if(get_post_meta($post->ID, "leetpress_good", true)) { ?><div class="pros"><p><?php echo get_post_meta($post->ID, "leetpress_good", true); ?></p></div><?php } ?>
                    <?php if(get_post_meta($post->ID, "leetpress_bad", true)) { ?><div class="cons"><p><?php echo get_post_meta($post->ID, "leetpress_bad", true); ?></p></div><?php } ?>

                </div>

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

可能最简单的方法是获取表上方的字段,并将其分配给变量:

$anime_anname = get_post_meta($post->ID, \'anime_anname\', true);
$anime_angenre = get_post_meta($post->ID, \'anime_angenre\', true);
...
然后,只需将该表包装在如下所示的if语句中:

if (
    !empty($anime_anname)
    || !empty($anime_angenre)
    || ...
) {
// Table goes here.
}
当然,在您的表中,也要确保使用变量,而不是再次获取提要。

希望这会有所帮助。

结束

相关推荐

搜索.php文件没有指向它应该指向的位置

嗨,我昨天开始创建我的第一个wordpress模板,今天我已经到了必须创建搜索页面结果的地步。从我在网上找到的应该是搜索。php文件。但出于某种原因,当我尝试搜索时,Wordpress似乎并没有指向那个页面。我还创建了searchform。php。以下是我的代码: <form id=\"searchform\" method=\"get\" action=\"<?php bloginfo(\'url\'); ?>\"> <input type=\