Domanda

I have this function, which gives me dropdown option:

public function edit_tax_image_field( $term ){
    $term_id = $term->term_id;
    $term_meta = get_option( "taxonomy_$term_id" );
    $image = $term_meta['tax_image'] ? $term_meta['tax_image'] : '';
?>
    <tr class="form-field">
        <th scope="row">
            <label for="term_meta[tax_image]">Dropdown menu</label>
            <td>
                <select name="term_meta[tax_image]" id="term_meta[tax_image]" style="width: 300px;">
    <?php
        $selected = $image;
        $p = '';
        $r = '';

        foreach ( _s_sample_select_options() as $option ) {
            $label = $option['label'];
            if ( $selected == $option['value'] ) // Make default first in list
                $p = "\n\t<option style=\"padding-right: 10px;\" selected='selected' value='" . esc_attr( $option['value'] ) . "'>$label</option>";
            else
                $r .= "\n\t<option style=\"padding-right: 10px;\" value='" . esc_attr( $option['value'] ) . "'>$label</option>";
        }
        echo $p . $r;
    ?></select>
                <p class="description">Some description.</p>
            </td>
        </th>
    </tr><!-- /.form-field -->
<?php
} // edit_tax_image_field

This is my saving function

public function save_tax_meta( $term_id ){

    if ( isset( $_POST['term_meta'] ) ) {

        $t_id = $term_id;
        $term_meta = array();

        $term_meta['tax_image'] = isset ( $_POST['term_meta']['tax_image'] ) ? esc_attr( $_POST['term_meta']['tax_image'] ) : '';

        // Save the option array.
        update_option( "taxonomy_$t_id", $term_meta );

    } // if isset( $_POST['term_meta'] )
} // save_tax_meta

I do not know, where should be bug. I am beginner in coding, so ... bug is maybe simple or not.

Solving this problem cost me one day :D and still have no solution :(

È stato utile?

Soluzione

try changing

if ( $selected == $option['value'] ) // Make default first in list
                $p = "\n\t<option style=\"padding-right: 10px;\" selected='selected' value='" . esc_attr( $option['value'] ) . "'>$label</option>";

to

if ( $selected == $option['value'] ) // Make default first in list
                $p .= "\n\t<option style=\"padding-right: 10px;\" selected='selected' value='" . esc_attr( $option['value'] ) . "'>$label</option>";
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top