2

更新:保存されているように見えますが、(保存後に) フォームが管理者にリロードされると、ドロップダウン ボックスに現在の値が表示されません。「選択済み」の行がありますが、現在の値を表示するために何が欠けているのかわかりません。

2 つの個別のドロップダウン メニューから CTA とランディング ページを選択できるウィジェットを作成しようとしています。ドロップダウンは、正常に機能している 2 つのカスタム投稿タイプに基づいて入力されます。私の問題は、値をデータベースに保存する方法がわからないことです (つまり、管理者のウィジェットで [保存] をクリックしても、値がデータベースにコミットされません。

cta_widget というウィジェット クラスの関連関数を次に示します。(2 番目のドロップダウンのコードは同一であるため省略しました)。

<?php
function update($new_instance, $old_instance)
{
    $instance = $old_instance;

    /* Strip tags (if needed) and update the widget settings. */
    $instance['cta_id'] = $new_instance['cta_id'];
    $instance['url_id'] = $new_instance['url_id'];

    return $instance;
}

function form($instance) {
    /* Set up some default widget settings. */
    $defaults = array('cta_id' => '23', 'url_id' => '28');
    $instance = wp_parse_args((array)$instance, $defaults);
?>
<p> 
    <label
        for="<?php echo $this->get_field_name('cta_id'); ?>"><?php _e('CTA:'); ?></label>

    <select name="<?php echo $this->get_field_name('cta_id'); ?>"
            id="cta_id<?php echo $this->get_field_name('cta_id'); ?>"
            style="width:100%;">
        <option value=""></option>

        <?php

        $posts = get_posts(
            array(
                'post_type'   => 'locable_ctas',
                'numberposts' => -1
            )
        );
        if ($posts) {

            foreach ($posts as $p) {
                if ($p == $selected) {
                    $selected = "selected = 'selected'";
                } else {
                    $selected = "";
                }
                echo '<option value="' . $p->ID . '" ' . $selected . '>' . $p->post_title . '</option>';
            }
        }

        ?>
    </select>
</p>
}
4

1 に答える 1