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'].' '.$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");') );
PS:如果你知道一个很好的教程,展示如何让你自己的选项页面/声明属性/制作自定义小部件,我很想听听你的意见.因为我所遵循的所有教程都是旧的,模糊的或太复杂的.
感谢你们!
| 归档时间: | 
 | 
| 查看次数: | 2272 次 | 
| 最近记录: |