0

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

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

这是我到目前为止所拥有的:

<div>
<table class="animetable">
<tr><th>Info</th></tr>
<tr><td><strong>Name: </strong><?php echo get_post_meta($post->ID, 'anime_anname', true); ?></td></tr>
<tr><td><strong>Genre: </strong><?php echo get_post_meta($post->ID, 'anime_angenre', true); ?></td></tr>
<tr><td><strong>Directed by: </strong><?php echo get_post_meta($post->ID, 'anime_andirector', true); ?></td></tr>
<tr><td><strong>Music by: </strong><?php echo get_post_meta($post->ID, 'anime_anmusic', true); ?></td></tr>
<tr><td><strong>Studio: </strong><?php echo get_post_meta($post->ID, 'anime_anstudio', true); ?></td></tr>
<tr><td><strong>Licensed ny: </strong><?php echo get_post_meta($post->ID, 'anime_anlicense', true); ?></td></tr>
<tr><td><strong>Network[s]: </strong><?php echo get_post_meta($post->ID, 'anime_annetwork', true); ?></td></tr>
<tr><td><strong>Original run: </strong><?php echo get_post_meta($post->ID, 'anime_anrun', true); ?></td></tr>
<tr><td><strong>Episodes: </strong><?php echo get_post_meta($post->ID, 'anime_anepisodes', true); ?></td></tr>
</table>
</div>

这是元框的代码:

$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>
4

2 回答 2

0

您可以尝试使用 jQuery,但您需要在字段的标题中添加一个tbodythead,因此它们不在您的<td>内联元素中。

thead为您的桌子调整一个

<table class="animetable">

<thead>
    <tr>
      <th>1</th>
      <th>2</th>
      <th>3</th>
      <th>4</th>
      <th>5</th>
      <th>6</th>
      <th>7</th>
      <th>8</th>
      <th>9</th>
      <th>10</th>
    </tr>
 </thead>

<tbody>
    <tr><td><?php echo get_post_meta($post->ID, 'anime_anname', true); ?></td></tr>
    <tr><td><?php echo get_post_meta($post->ID, 'anime_angenre', true); ?></td></tr>
    <tr><td><?php echo get_post_meta($post->ID, 'anime_andirector', true); ?></td></tr>
    <tr><td><?php echo get_post_meta($post->ID, 'anime_anmusic', true); ?></td></tr>
    <tr><td><?php echo get_post_meta($post->ID, 'anime_anstudio', true); ?></td></tr>
    <tr><td><?php echo get_post_meta($post->ID, 'anime_anlicense', true); ?></td></tr>
    <tr><td><?php echo get_post_meta($post->ID, 'anime_annetwork', true); ?></td></tr>
    <tr><td><?php echo get_post_meta($post->ID, 'anime_anrun', true); ?></td></tr>
    <tr><td><?php echo get_post_meta($post->ID, 'anime_anepisodes', true); ?></td></tr>

</tbody>
</table>

使用 jQuery 函数隐藏空表:

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>

 <script>
    $(function() {
        $('.animetable tbody:empty').hide();
    });
 </script>

替代:

 <script>
    $(function() {
        var emptyTest = $('.animetable tbody').is(:empty);
        if (emptyTest == true) {
            $('.animetable').hide();
        } else {

        }
    });
 </script>

替代方案是使用 PHP,但我没有足够的信息来说明您如何获取数据。

于 2012-08-21T13:01:54.987 回答
0

这个怎么样?您可以放置​​一个条件语句来检查数据是否可用。如果是这样,请显示该表。

<div>

**Edit**
<?php 
   $nameSet = isset(get_post_meta($post->ID, 'anime_anname', true));
   $genreSet = isset(get_post_meta($post->ID, 'anime_angenre', true));
   $directorSet = isset(get_post_meta($post->ID, 'anime_andirector', true));
   /*same for all*/

if($nameSet && $genreSet && $directorSet && ..etc etc){ ?>

<table class="animetable">
<tr><th>Info</th></tr>
<tr><td><strong>Name: </strong><?php echo get_post_meta($post->ID, 'anime_anname', true); ?></td></tr>
<tr><td><strong>Genre: </strong><?php echo get_post_meta($post->ID, 'anime_angenre', true); ?></td></tr>
<tr><td><strong>Directed by: </strong><?php echo get_post_meta($post->ID, 'anime_andirector', true); ?></td></tr>
<tr><td><strong>Music by: </strong><?php echo get_post_meta($post->ID, 'anime_anmusic', true); ?></td></tr>
<tr><td><strong>Studio: </strong><?php echo get_post_meta($post->ID, 'anime_anstudio', true); ?></td></tr>
<tr><td><strong>Licensed ny: </strong><?php echo get_post_meta($post->ID, 'anime_anlicense', true); ?></td></tr>
<tr><td><strong>Network[s]: </strong><?php echo get_post_meta($post->ID, 'anime_annetwork', true); ?></td></tr>
<tr><td><strong>Original run: </strong><?php echo get_post_meta($post->ID, 'anime_anrun', true); ?></td></tr>
<tr><td><strong>Episodes: </strong><?php echo get_post_meta($post->ID, 'anime_anepisodes', true); ?></td></tr>
</table>

<?php } ?> 

</div>
于 2012-08-21T04:52:36.993 回答