自定义窗口小部件不将值保存到数据库

时间:2013-08-25 00:43:15

标签: php wordpress

更新:看起来他们正在保存,但是当在admin中重新加载表单时,当前值不会显示在下拉框中(保存后)。我有“选中”的行,但我不知道我缺少什么才能让他们显示当前值。

我正在尝试创建一个小部件,允许您从两个单独的下拉菜单中选择一个CTA和一个着陆页。根据两个自定义帖子类型填充下拉列表,这些类型工作正常。我的问题是我无法弄清楚如何将值保存到数据库(即,在管理员中单击窗口小部件上的保存不会将值提交到数据库。

以下是我的widget类中称为cta_widget的相关函数。 (我省略了第二个下拉列表的代码,因为它完全相同)。

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

1 个答案:

答案 0 :(得分:0)

我明白了。以下是其他任何有相同问题的代码。

function form( $instance ) { 

        $cta = $instance['cta_id']; 
        $lp = $instance['lp_id'];       

        /* Set up some default widget settings. */  
        $defaults = array( 'cta_id' => '', 'lp_id' => '');  
        $instance = wp_parse_args( (array) $instance, $defaults );   

        ?>

        <p>  

        <?php echo 'CTA: '.$cta;?>
        <?php echo 'LP: '.$lp.'<br/>';?>

        <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->ID==$cta)
                    {
                        $is_selected = "selected = 'selected'";
                    }
                    else
                    {
                        $is_selected = "";
                    }
                    echo '<option value="' . $p->ID . '" '.$is_selected.'>' .$p->post_title  . '</option>';
                }
            } 

        ?>
        </select>

        </p>

        <p>  

        <label for="<?php echo $this->get_field_name( 'lp_id' ); ?>"><?php _e( 'LP:' ); ?></label> 

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

            <?php

            $posts = get_posts(
                array(
                    'post_type'  => 'locable_lps',
                    'numberposts' => -1
                )
            );
            if($posts)
            {
                foreach( $posts as $p )
                {
                    if ($p->ID==$lp)
                    {
                        $is_selected = "selected = 'selected'";
                    }
                    else
                    {
                        $is_selected = "";
                    }
                    echo '<option value="' . $p->ID . '" '.$is_selected.'>' .$p->post_title  . '</option>';
                }
            } 

        ?>
        </select>

        </p>            

        <?php  
    }  
相关问题