0

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: EDIT*

    <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.
}

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>

1 Answer 1

1

Probably the easiest thing to do would be to fetch the fields above your table, and assign them to variables:

$anime_anname = get_post_meta($post->ID, 'anime_anname', true);
$anime_angenre = get_post_meta($post->ID, 'anime_angenre', true);
...

Then just wrap the table in an if statement that looks like the following:

if (
    !empty($anime_anname)
    || !empty($anime_angenre)
    || ...
) {
// Table goes here.
}

Of course within your table, be sure to use the variables as well, rather than fetching the feeds again.

Hope that this helps.

2
  • alright I have whats in the edit above so far. Excuse me, I am a PHP novice :D. How do I call the variables? Commented Aug 22, 2012 at 4:24
  • I'm assuming you're out of PHP mode here, that you did a ?> after the opening brace of the if: You would just do something like <input type="text" value="<?php echo $anime_anname; ?>" name=... /> That's the easiest way to do it. You're just echoing the values of the variables you created on the page wherever you need them.
    – Aaron
    Commented Aug 22, 2012 at 11:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.