2012-01-24 12 views
0

私は自分のカスタムポストタイプにドロップダウン値を保存しようとしています。私は間違って何をしていますか?ワードプレスのドロップダウン値を保存する

は、マークアップ:

<li><label>Location</label><input name="tf_events_location" value="<?php echo $meta_loc; ?>" /></li> 
<li><label>Band relation</label> 
    <select name="tf_events_relate" id="tf_events_relate"> 
     <option value="">All Artists </option> 
     <option value="146">Mormor</option> 
     <option value="140">John Face</option> 
    </select> 
</li> 

そして

function save_tf_events(){ 

global $post; 

if(!isset($_POST["tf_events_location"])): 
    return $post; 
    endif; 
    $updateloc = $_POST["tf_events_location"]; 
    update_post_meta($post->ID, "tf_events_location", $updateloc); 

if(!isset($_POST["tf_events_relate"])): 
    return $post; 
    endif; 
    $updaterelate = $_POST["tf_events_relate"]; 
    update_post_meta($post->ID, "tf_events_relate", $updaterelate); 
} 

を保存マイtf_events_locationオールライト保存されますが、私のtf_events_relateは何もしていません。
私は原則間違っを取得するかもしれないと思う;)

+0

でそれを簡素化することができ、あなたが 'tf_events_relate'フィールドの通過値を取得していますか? – cdeszaq

+0

私がオプションを選択した場合、私はその価値を得なければなりません - そうですか? –

+0

"All Artists"を選択した場合、価値は得られませんが、 "John Face"を選択すると、値として「140」が得られます。 – cdeszaq

答えて

1

問題は、単にページが戻ってきたときに選択されているもの<option><select>要素を語っていないことです。

はその、他よりもいくつかのより柔軟な作業を行うための別の方法はいくつかありますが、それはあなたが上記の持っているhtmlコードで最もシンプルな形式だで、これは動作します:それはないです

<li><label>Location</label><input name="tf_events_location" value="<?php echo $meta_loc; ?>" /></li> 
<li><label>Band relation</label> 
    <select name="tf_events_relate" id="tf_events_relate"> 
     <option <?php ($meta_relate == "") ? echo("selected "):; ?>value="">All Artists </option> 
     <option <?php ($meta_relate == "146") ? echo("selected "):; ?>value="146">Mormor</option> 
     <option <?php ($meta_relate == "140") ? echo("selected "):; ?>value="140">John Face</option> 
    </select> 
</li> 

最もエレガントですが、うまくいくはずです。

+0

ありがとう:)魅力としての半作品 - しかし、私はここからそれを理解する:) –

0

cdeszaqの答えは仕事を得るだろうが、あなたはselected()

<li><label>Location</label> 
    <input name="tf_events_location" value="<?php echo $meta_loc; ?>" /> 
</li> 
<li><label>Band relation</label> 
    <select name="tf_events_relate" id="tf_events_relate"> 
     <option value="" <?php selected($meta_relate, ''); ?>All Artists</option> 
     <option value="146" <?php selected($meta_relate, '146'); ?>Mormor</option> 
     <option value="140" <?php selected($meta_relate, '140'); ?>John Face</option> 
    </select> 
</li> 
関連する問題