回声选择值Wordpress

时间:2015-11-17 16:57:32

标签: php wordpress select

我坚持这个。我无法获得所选选项以在Wordpress的后端显示所选项目。它保存到数据库,我可以在前端回应它。现在把我的头发拉出来。

<?php add_action( 'add_meta_boxes', 'dynamic_add_custom_box' );

/* Do something with the data entered */
add_action( 'save_post', 'dynamic_save_postdata' );

/* Adds a box to the main column on the Post and Page edit screens */
function dynamic_add_custom_box() {
add_meta_box(
    'dynamic_sectionid',
    __( 'Plot Status', 'myplugin_textdomain' ),
    'dynamic_inner_custom_box',
    'house_type');
}

/* Prints the box content */
function dynamic_inner_custom_box() {
global $post;
// Use nonce for verification
wp_nonce_field( plugin_basename( __FILE__ ), 'dynamicMeta_noncename' );
?>
<div id="meta_inner">
<?php

//get the saved meta as an arry
$plots = get_post_meta($post->ID,'plots',true);

$c = 0;
if ( count( $plots ) > 0 ) {
    foreach( $plots as $plotno ) {
        if ( isset( $plotno['title'] ) || isset( $plotno['plotno'] ) || isset( $plotno['development'] ) ) {
            printf( '
            <p>Plot Number: <input type="text" name="plots[%1$s][title]" value="%2$s" /> 
               Status : <select name="plots[%1$s][plotno]" value="%3$s">
               <option value="Not Released" '. selected( 'plots[%1$s][plotno]', "Not Released" ).'>Not Released</option>
               <option value="Available" '. selected( 'plots[%1$s][plotno]', "Available" ) .'>Available</option>
               <option value="Reserved" '. selected( 'plots[%1$s][plotno]', "Reserved" ) .'>Reserved</option>
               <option value="Sold" '. selected( 'plots[%1$s][plotno]', "Sold" ) .'>Sold</option>
               </select>
               Development : <input type="text" name="plots[%1$s][development]" value="%4$s" />
               <span class="remove" style="color:red;cursor:pointer;padding-left:10px;">%5$s</span></p>', $c, $plotno['title'], $plotno['plotno'], $plotno['development'], __( 'Remove' ) );
            $c = $c +1;
        }
    }
}


?>
<span id="here"></span>
<div class="button">
<span class="add"><?php _e('Add Plot Number'); ?></span></div>
<script>
var $ =jQuery.noConflict();
$(document).ready(function() {
    var count = <?php echo $c; ?>;
    $(".add").click(function() {
        count = count + 1;

        $('#here').append('<p> Plot Number: <input type="text" name="plots['+count+'][title]" value="" /> Status : <select name="plots['+count+'][plotno]" value=""><option value="" >Not Released</option><option value="">Available</option><option value="">Reserved</option>   <option value="">Sold</option> </select> Development : <input type="text" name="plots['+count+'][development]" value="" /> <span style="remove" >Remove</span></p>' );
        return false;
    });
    $(".remove").live('click', function() {
        $(this).parent().remove();
    });
});
</script>
</div><?php

}

我所做的是我尝试通过添加下拉选项来编辑它。 tutorial

1 个答案:

答案 0 :(得分:0)

我是用脚本做的。它的工作

enter image description here

                add_action( 'add_meta_boxes', 'dynamic_add_custom_box' );

                /* Do something with the data entered */
                add_action( 'save_post', 'dynamic_save_postdata' );

                /* Adds a box to the main column on the Post and Page edit screens */
                function dynamic_add_custom_box() {
                    add_meta_box(    'dynamic_sectionid', __( 'Plot Status', 'myplugin_textdomain' ),  'dynamic_inner_custom_box','post');
                }

                /* Prints the box content */
                function dynamic_inner_custom_box() {
                global $post;
                // Use nonce for verification
                wp_nonce_field( plugin_basename( __FILE__ ), 'dynamicMeta_noncename' );
                ?>
                <div id="meta_inner">
                <?php

                //get the saved meta as an arry
                $plots = get_post_meta($post->ID,'plots',true);

                $c = 0;
                if ( count( $plots ) > 0 ) {
                    foreach( $plots as $plotno ) {
                        if ( isset( $plotno['title'] ) || isset( $plotno['plotno'] ) || isset( $plotno['development'] ) ) {

                            printf( '
                            <p>Plot Number: <input type="text" name="plots[%1$s][title]" value="%2$s" /> 
                               Status : <select name="plots[%1$s][plotno]" class="plotnoclass_%1$s">
                               <option value="Not Released">Not Released</option>
                               <option value="Available">Available</option>
                               <option value="Reserved">Reserved</option>
                               <option value="Sold">Sold</option>
                               </select>
                               Development : <input type="text" name="plots[%1$s][development]" value="%4$s" />
                               <span class="remove" style="color:red;cursor:pointer;padding-left:10px;">%5$s</span></p>', $c, $plotno['title'], $plotno['plotno'], $plotno['development'], __( 'Remove' ) );
                               echo "<script>jQuery(\".plotnoclass_$c\").val('".$plotno['plotno']."');</script>";
                            $c = $c +1;
                        }
                    }
                }


                ?>
                <span id="here"></span>
                <div class="button">
                <span class="add"><?php _e('Add Plot Number'); ?></span></div>
                <script>
                var $ =jQuery.noConflict();
                $(document).ready(function() {
                    var count = <?php echo $c; ?>;
                    $(".add").click(function() {
                        count = count + 1;

                        $('#here').append('<p> Plot Number: <input type="text" name="plots['+count+'][title]" value="" /> Status : <select name="plots['+count+'][plotno]" value=""><option value="" >Not Released</option><option value="">Available</option><option value="">Reserved</option>   <option value="">Sold</option> </select> Development : <input type="text" name="plots['+count+'][development]" value="" /> <span style="remove" >Remove</span></p>' );
                        return false;
                    });
                    $(".remove").live('click', function() {
                        $(this).parent().remove();
                    });
                });
                </script>
                </div><?php

                }