Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Wordpress自定义小部件记住多个选择选项_Php_Widget_Multipleselection_Wordpress - Fatal编程技术网

Php Wordpress自定义小部件记住多个选择选项

Php Wordpress自定义小部件记住多个选择选项,php,widget,multipleselection,wordpress,Php,Widget,Multipleselection,Wordpress,我正在为我们的网站编写一个自定义小部件来显示一些选定的帖子。在管理部分,我有一个多选择框,让管理员根据自己的名字选择多篇文章。这很好,但当我选择几个不同的帖子并保存时,什么都没有保存 有人能解释一下吗 这是我的密码 <?php /* Plugin Name: Hot Topics Plugin URI: http://www.weddingideasmag.com Description: Use this widget to choose an array of posts snipp

我正在为我们的网站编写一个自定义小部件来显示一些选定的帖子。在管理部分,我有一个多选择框,让管理员根据自己的名字选择多篇文章。这很好,但当我选择几个不同的帖子并保存时,什么都没有保存

有人能解释一下吗

这是我的密码

<?php
/* 
Plugin Name: Hot Topics
Plugin URI: http://www.weddingideasmag.com
Description: Use this widget to choose an array of posts snippets to show
Version: 1.0)
Author: James Payne
Author URI: http://www.bluntcreative.co.uk
License: GPL2
*/


class HotTopics extends WP_Widget {

// constructor
function HotTopics() {
    $widget_ops = array( 'name' => 'Hot Topics','classname' => 'widget-hot-topics', 'description' => __( "Use this widget to choose an array of posts snippets to show in the sidebar." ) );
    $this->WP_Widget( 'hottopics', __('Hot Topics'), $widget_ops);
}

// widget form creation
function form($instance) {  
    // Check values
    if( $instance) {
        $select = esc_attr($instance['select']); // Added 
    } else {
         $select ='';
    }
    ?>

    <select multiple="multiple" name="<?php echo $this->get_field_name('select'); ?>[]" id="<?php echo $this->get_field_id('select'); ?>" class="widefat" size="15" style="margin-bottom:15px;">
        <?php
        $args = array( 'offset'=> 1, 'orderby' => 'date', 'order' => 'DESC', 'posts_per_page' => 200, 'post_status' => 'publish' );

        // The Query
        query_posts( $args );

        // The Loop
        while ( have_posts() ) : the_post();

        $title = get_the_title();
        ?>
            <option value="<?php echo get_the_ID();?>" class="hot-topic" <?php $select == $title ? ' selected="selected"' : '';?> style="margin-bottom:3px;">
                <?php echo $title;?>
            </option>
            <?php
        endwhile;

        // Reset Query
        wp_reset_query();
        ?>
    </select>

    <?php
}

// update widget
function update($new_instance, $old_instance) {
      $instance = $old_instance;
      // Fields
      $instance['select'] = strip_tags($new_instance['select']);
     return $instance;
}

// widget display
function widget($args, $instance) {
    /* ... */
    echo 'tetst';
}
}

// register widget
add_action('widgets_init', create_function('', 'return register_widget("HotTopics");'));

?>

当小部件更新时,
strip\u标签
正在销毁所选帖子的数组。一个
esc\u sql
完成这项工作。另外,
query\u posts
。最后,存储文章标题并不理想,因为它可能会改变,ID是永久的

工作样本:

# PHP 5.3+ anonymous function
add_action( 'widgets_init', function() {
    register_widget( 'Sample_Widget_SO_19246434' );
});

class Sample_Widget_SO_19246434 extends WP_Widget 
{
    function Sample_Widget_SO_19246434() 
    {
        $this->WP_Widget( 
            'hottopics', 
            __('Hot Topics'),
            array( 
                'name' => 'Hot Topics',
                'classname' => 'widget-hot-topics', 
                'description' => __( "Description" ) 
            )
        );
    }

    function form( $instance ) 
    {
        if( $instance ) 
            $select = $instance['select'];
        else
             $select ='';

        $get_posts = get_posts( array( 
            'offset'=> 1, 
            'orderby' => 'date', 
            'order' => 'DESC', 
            'posts_per_page' => 200, 
            'post_status' => 'publish' 
        ));
        if( $get_posts )
        {
            printf(
                '<select multiple="multiple" name="%s[]" id="%s" class="widefat" size="15">',
                $this->get_field_name('select'),
                $this->get_field_id('select')
            );
            foreach( $get_posts as $post )
            {
                printf(
                    '<option value="%s" class="hot-topic" %s style="margin-bottom:3px;">%s</option>',
                    $post->ID,
                    in_array( $post->ID, $select) ? 'selected="selected"' : '',
                    $post->post_title
                );
            }
            echo '</select>';
        }
        else
            echo 'No posts found :(';
    }

    function update( $new_instance, $old_instance ) 
    {
        $instance = $old_instance;
        $instance['select'] = esc_sql( $new_instance['select'] );
        return $instance;
    }

    function widget( $args, $instance ) 
    {
        echo 'Hello world';
    }
}
#PHP5.3+匿名函数
添加操作('widgets_init',函数(){
注册小部件(“示例小部件”SO 19246434”);
});
类示例_Widget_SO_19246434扩展了WP_Widget
{
函数示例_Widget_SO_19246434()
{
$this->WP\u小部件(
"热门话题",,
__(‘热门话题’),
数组(
“名称”=>“热门话题”,
'classname'=>'widget热门话题',
“说明”=>u uu(“说明”)
)
);
}
函数形式($instance)
{
如果($实例)
$select=$instance['select'];
其他的
$select='';
$get_posts=get_posts(数组(
“偏移量”=>1,
'orderby'=>'date',
“订单”=>“描述”,
“每页帖子数”=>200,
“发布状态”=>“发布”
));
如果($get_posts)
{
printf(
'',
$this->get_field_name('select'),
$this->get_field_id('select'))
);
foreach($get_posts as$post)
{
printf(
“%s”,
$post->ID,
在数组中($post->ID,$select)?'selected=“selected”:“,
$post->post_标题
);
}
回声';
}
其他的
回音“未找到帖子:(”;
}
函数更新($new\u实例,$old\u实例)
{
$instance=$old_实例;
$instance['select']=esc_sql($new_instance['select']);
返回$instance;
}
函数小部件($args$instance)
{
回声“你好世界”;
}
}
相关的