dsjhejw3232 2015-11-17 16:57
浏览 63
已采纳

回声选择值Wordpress

I'm stuck with this. I can't get the selected option to display the selected item in the backend of Wordpress. It saves to the database and I can echo it out on the front end. Pulling my hair out now.

<?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

}

What I've done is I've tried to edit this by adding a drop down select option. tutorial

  • 写回答

1条回答 默认 最新

  • 普通网友 2015-11-18 11:02
    关注

    I did this in script. Its working

    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
    
                    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应