我试图做我自己的部件。但我发现这并不容易。我试图设置默认值,但我不知道如何做到这一点。WordPress的如何设置窗口小部件的默认值?
是的,我GOOGLE了很多。我几乎想要1个星期来获得我自己的选项页面和自定义小部件,但我没有成功。
所以回到我的问题,这是我现在的代码:
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:如果你知道一个很好的教程,说明如何使你自己的选项页/声明属性/ makeing用户控件,我会期待您的来信。因为我所遵循的所有教程都很陈旧,模糊或太复杂。
谢谢你们!
很抱歉通知,但您一直在错误的教程,[这一个](http://justintadlock.com/archives/2009/05/26/the-complete-guide-to-creating-widgets- in-wordpress-28)已经快5岁了,现在依然存在;)Justin Tadlock是一个重量级的开发者。此外,而不是谷歌,首先在[wordpress.se]进行研究。哦,WPTuts +也有很多不错的东西。 – brasofilo
你好brasofilo,我知道我跟随的馅饼是垃圾:(我会尝试这一个坦克为你的反应 – apero