如何在自定义小工具WordPress中获取选择框的值

时间:2016-08-31 作者:Kallol Das

我正在尝试制作一个自定义小部件。我的代码如下:

class techno_widget extends WP_Widget {

    function __construct() {
    parent::__construct(

    \'techno_widget\', 

     __(\'Recent Full Post\', \'techno_widget_domain\'), 

    array( \'description\' => __( \'A full post will be appeared on Sidebar\', \'techno_widget_domain\' ), ) 
);
}

public function widget( $args, $instance ) {
$title = apply_filters( \'widget_title\', $instance[\'title\'] );
$blog-title = $instance[\'blog-title\'];

echo $args[\'before_widget\'];
if ( ! empty( $title ) )
echo $args[\'before_title\'] . $title . $args[\'after_title\'];

echo $instance[\'blog-title\'];                   

echo $args[\'after_widget\'];
}

public function form( $instance ) {
if ( isset( $instance[ \'title\' ] ) ) {
$title = $instance[ \'title\' ];
}
else {
$title = __( \'New title\', \'wpb_widget_domain\' );
}

$blog-title = $instance[ \'blog-title\' ];

?>
<p>
<label for="<?php echo $this->get_field_id( \'blog-title\' ); ?>"><?php  _e( \'Select Title:\' ); ?></label> 
<select class="widefat" id="<?php echo $this->get_field_id( \'blog-title\' ); ?>" name="<?php echo $this->get_field_name( \'blog-title\' ); ?>">

<?php  

                $fullpost = new WP_Query(array(
                    \'post_type\' => \'post\',                      
                ));

if($fullpost->have_posts()): while($fullpost->have_posts()): $fullpost->the_post(); ?>

    <option value="<?php the_title();?>"><?php the_title();?></option>

<?php endwhile; endif;?>

</select>
</p>
<p>
<label for="<?php echo $this->get_field_id( \'title\' ); ?>"><?php _e( \'Title:\' ); ?></label> 
<input class="widefat" id="<?php echo $this->get_field_id( \'title\' ); ?>" name="<?php echo $this->get_field_name( \'title\' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>
<?php 
}

public function update( $new_instance, $old_instance ) {
$instance = array();
$instance[\'title\'] = ( ! empty( $new_instance[\'title\'] ) ) ? strip_tags( $new_instance[\'title\'] ) : \'\';
return $instance;
}

function techno_load_widget() {
register_widget( \'techno_widget\' );
}
add_action( \'widgets_init\', \'techno_load_widget\' );
现在wordpress显示错误。它告诉我们,

分析错误:语法错误,在C:\\xampp\\htdocs\\wpreference\\wp content\\themes\\lawyer\\functions中出现意外的“=”。php在线87

我的代码有什么问题?如何获取选择框的值?

1 个回复
SO网友:CodeMascot

你错过了班上最后的大括号techno_widget, 您需要调用函数techno_load_widget 课外活动techno_widget 您声明了变量“$blog title”wrong. You can\'t declare variable with-. It is core PHP `约定。所以我重写你的代码如下-

class techno_widget extends WP_Widget {

    function __construct() {
        parent::__construct(

            \'techno_widget\',

            __(\'Recent Full Post\', \'techno_widget_domain\'),

            array( \'description\' => __( \'A full post will be appeared on Sidebar\', \'techno_widget_domain\' ), )
        );
    }

    public function widget( $args, $instance ) {
        $title = apply_filters( \'widget_title\', $instance[\'title\'] );
        $blog_title = $instance[\'blog_title\'];

        echo $args[\'before_widget\'];
        if ( ! empty( $title ) )
            echo $args[\'before_title\'] . $title . $args[\'after_title\'];

        echo $instance[\'blog_title\'];

        echo $args[\'after_widget\'];
    }

    public function form( $instance ) {
        if ( isset( $instance[ \'title\' ] ) ) {
            $title = $instance[ \'title\' ];
        }
        else {
            $title = __( \'New title\', \'wpb_widget_domain\' );
        }

        $blog_title = $instance[ \'blog_title\' ];

        ?>
        <p>
            <label for="<?php echo $this->get_field_id( \'blog_title\' ); ?>"><?php  _e( \'Select Title:\' ); ?></label>
            <select class="widefat" id="<?php echo $this->get_field_id( \'blog_title\' ); ?>" name="<?php echo $this->get_field_name( \'blog_title\' ); ?>">

                <?php

                $fullpost = new WP_Query(array(
                    \'post_type\' => \'post\',
                ));

                if($fullpost->have_posts()): while($fullpost->have_posts()): $fullpost->the_post(); ?>

                    <option value="<?php the_title();?>"><?php the_title();?></option>

                <?php endwhile; endif;?>

            </select>
        </p>
        <p>
            <label for="<?php echo $this->get_field_id( \'title\' ); ?>"><?php _e( \'Title:\' ); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id( \'title\' ); ?>" name="<?php echo $this->get_field_name( \'title\' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
        </p>
        <?php
    }

    public function update( $new_instance, $old_instance ) {
        $instance = array();
        $instance[\'title\'] = ( ! empty( $new_instance[\'title\'] ) ) ? strip_tags( $new_instance[\'title\'] ) : \'\';
        return $instance;
    }
}
function techno_load_widget() {
    register_widget( \'techno_widget\' );
}

add_action( \'widgets_init\', \'techno_load_widget\' );
这很有效。这是截图-enter image description here

现在,您可以根据需要修改此代码。但我想你以前$instance[\'blog-title\'](在我的代码中$instance[\'blog_title\']) 也许你可以用$instance[\'title\'].

您可以从任何其他输入字段获取输入。有关更多信息,请在谷歌上搜索。

相关推荐

My widgets do not save

每次我保存我的小部件并离开页面时,我的小部件都会消失。侧边栏已完全清空,不会保存任何更改。控制台或PHP日志中没有任何错误。如果我将小部件直接复制并保存在数据库中widgets_text, 它们将被显示,但我仍然无法在侧边栏中添加或删除任何内容。这只发生在我的右侧边栏上,左侧边栏工作正常,但它们都以相同的方式注册。这是我注册侧边栏的方式:function my_widgets_init() { register_sidebar( array ( \'name\'