Wordpress如何设置小部件的默认值?

ape*_*ero 0 wordpress wordpress-theming wordpress-plugin

我试图制作自己的小部件.但我发现这并不容易.我正在尝试设置默认值,但我不知道如何做到这一点.

是的,我谷歌搜索了很多.我差不多试了一个星期来获得我自己的选项页面和自定义小部件,但我没有成功.

所以回到我的问题,现在这是我的代码:

class Superdeal_Widget extends WP_Widget {

public function __construct()
{
    parent::__construct(
        'Superdeal-widget',
        'Superdeal Widget',
        array(
            'description' => 'Superdeal widget'
        ),
        array (
            'width' => 400,
            'height' => 350
        )
    );
}  

public function widget( $args, $instance )
  {
    // basic output just for this example
    echo '<p><a href="'.$instance['url'].'">'.$instance['titel'].'&nbsp;'.$instance['superdeal'].'</a></p>';

  }

  public function form( $instance )
  {
    // removed the for loop, you can create new instances of the widget instead
    ?>
     <p>
      <label for="<?php echo $this->get_field_id('titel'); ?>">Titel</label><br />
      <input type="text" name="<?php echo $this->get_field_name('titel'); ?>" id="<?php echo $this->get_field_id('titel'); ?>-title" value="<?php echo $instance['titel']; ?>" class="widefat" />
    </p>
    <p>
      <label for="<?php echo $this->get_field_id('url'); ?>">Url</label><br />
      <input type="text" name="<?php echo $this->get_field_name('url'); ?>" id="<?php echo $this->get_field_id('url'); ?>-url" value="<?php echo $instance['url']; ?>" class="widefat" />
    </p>
    <p>
      <label for="<?php echo $this->get_field_id('superdeal'); ?>">Superdeal tekst</label><br />
      <input type="text" name="<?php echo $this->get_field_name('superdeal'); ?>" id="<?php echo $this->get_field_id('superdeal'); ?>-title" value="<?php echo $instance['superdeal']; ?>" class="widefat" />
    </p>
    <?php
  }


} 
// end class

// init the widget
add_action( 'widgets_init', create_function('', 'return register_widget("Superdeal_Widget");') );
Run Code Online (Sandbox Code Playgroud)

PS:如果你知道一个很好的教程,展示如何让你自己的选项页面/声明属性/制作自定义小部件,我很想听听你的意见.因为我所遵循的所有教程都是旧的,模糊的或太复杂的.

感谢你们!

loQ*_*loQ 5

这应该可以帮助你→ widget教程

注意这一行来自form()函数

$ instance = wp_parse_args((array)$ instance,array('title'=>'DEFAULT_VALUE_HERE'));

您可以添加几个实例 array( 'title' => 'DEFAULT_VALUE_HERE', 'name' => 'DEFAULT_VALUE_HERE' )

希望这会帮助你前进.