Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to display an extra meta field for taxonomy terms. wever I'm trying to create a conditional, in that, if the meta field is empty, show a default value.

here's the code for creating the taxonomy meta field:

// Add phone number to location taxonomies
function nwtd_lpfs_taxonomy_add_new_meta_field() {
    ?>
    <div class="form-field">
        <label for="term_meta[phone]"><?php _e( 'Location Based Phone Number', 'nwtd' ); ?></label>
        <input type="text" name="term_meta[phone]" id="term_meta[phone]" value="">
        <p class="description"><?php _e( 'Enter a phone number for this location','nwtd' ); ?></p>
    </div>
<?php
}
add_action( 'locations_add_form_fields', 'nwtd_lpfs_taxonomy_add_new_meta_field', 10, 2 );

// Edit term page
function nwtd_lpfs_taxonomy_edit_meta_field($term) {

    // put the term ID into a variable
    $t_id = $term->term_id;

    // retrieve the existing value(s) for this meta field. This returns an array
    $term_meta = get_option( "taxonomy_$t_id" ); ?>
    <tr class="form-field">
    <th scope="row" valign="top"><label for="term_meta[phone]"><?php _e( 'Location Based Phone Number', 'nwtd' ); ?></label></th>
        <td>
            <input type="text" name="term_meta[phone]" id="term_meta[phone]" value="<?php echo esc_attr( $term_meta['phone'] ) ? esc_attr( $term_meta['phone'] ) : ''; ?>">
            <p class="description"><?php _e( 'Enter a phone number for this location','nwtd' ); ?></p>
        </td>
    </tr>
<?php
}
add_action( 'locations_edit_form_fields', 'nwtd_lpfs_taxonomy_edit_meta_field', 10, 2 );

// Save extra taxonomy fields callback function.
function save_taxonomy_custom_meta( $term_id ) {
    if ( isset( $_POST['term_meta'] ) ) {
        $t_id = $term_id;
        $term_meta = get_option( "taxonomy_$t_id" );
        $cat_keys = array_keys( $_POST['term_meta'] );
        foreach ( $cat_keys as $key ) {
            if ( isset ( $_POST['term_meta'][$key] ) ) {
                $term_meta[$key] = $_POST['term_meta'][$key];
            }
        }
        // Save the option array.
        update_option( "taxonomy_$t_id", $term_meta );
    }
}  
add_action( 'edited_locations', 'save_taxonomy_custom_meta', 10, 2 );  
add_action( 'create_locations', 'save_taxonomy_custom_meta', 10, 2 );

The conditional I'm trying to use in my template is:

<?php if( $term_meta['phone'] != "" ) {             
     echo $term_meta['phone'];                                      } else {                                
    echo '(555) 555-555';                                       }?>

Any thoughts? TIA

share|improve this question
    
What doesn't work? Does it not save? Does it save but not display? What? –  s_ha_dum Jun 13 '13 at 17:05
    
It saves. It does not display. –  NW Tech Jun 13 '13 at 17:08

1 Answer 1

up vote 0 down vote accepted

I ended up adding an additional taxonomy meta class

I was then able to use the following to get my phone number:

<?php               
                    //Get the correct taxonomy ID by slug
                    $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );

                    //Get Taxonomy Meta
                    $saved_data = get_tax_meta($term->term_id,'loc_phone');
                ?>
                <?php if( $saved_data != "" ) {

                    echo $saved_data; 

                } else {

                    echo '(555) 555-555'; 

                }?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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