2013-10-14 79 views
0

我试图做我自己的部件。但我发现这并不容易。我试图设置默认值,但我不知道如何做到这一点。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'].'&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");')); 

PS:如果你知道一个很好的教程,说明如何使你自己的选项页/声明属性/ makeing用户控件,我会期待您的来信。因为我所遵循的所有教程都很陈旧,模糊或太复杂。

谢谢你们!

+0

很抱歉通知,但您一直在错误的教程,[这一个](http://justintadlock.com/archives/2009/05/26/the-complete-guide-to-creating-widgets- in-wordpress-28)已经快5岁了,现在依然存在;)Justin Tadlock是一个重量级的开发者。此外,而不是谷歌,首先在[wordpress.se]进行研究。哦,WPTuts +也有很多不错的东西。 – brasofilo

+0

你好brasofilo,我知道我跟随的馅饼是垃圾:(我会尝试这一个坦克为你的反应 – apero

回答

4

这会帮助你→widget tutorial

注意在这条线的形式()函数

$实例= wp_parse_args((阵列)$例如,阵列( '标题'=> 'DEFAULT_VALUE_HERE' ));

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

希望这会帮助你去。

+0

太好了!谢谢;) – apero

+0

如果这能解决你的问题,请接受这个答案。谢谢! – loQ

相关问题