I made a table out of the fields in a custom meta box I created, but now I'd like to know if I could have the table disappear if none of the fields were filled in.
Is this too complicated, is there a better solution, or should I just try to fill in all the fields as much as possible?
This is what I have so far:
<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>
This is the code for the meta box:
$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);
}
}
}
And finally, the creator of the theme did something similar, where the fields would disappear if nothing was entered. Here is what he did:
<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>