【问题标题】:Wordpress Widget form functionWordpress Widget 表单功能
【发布时间】:2015-05-04 14:37:40
【问题描述】:

我的 Wordpress 小部件中有这 2 个功能,但我无法使用表单功能来获取小部件选项的默认值或保存值。

private static function get_defaults() {
    $defauls = array(
            'title' => '' , 
            'subtitle' => '' ,
            'columns' => '4' ,
            'ntax' => '' ,
            'showposts' => '12' ,
            'imagesize' => 'medium' ,
            'posttype' => '' ,
            'exclude' => '' ,
            'terms' => '' ,
            'border' => '' ,
            'padding' => ''
        );

    return $defaults;
}
/**
 * Generates the administration form for the widget.
 *
 * @param array instance The array of keys and values for the widget.
 */
public function form( $instance ) {
    $instance = wp_parse_args(
        (array)$instance,self::get_defaults()
    );

【问题讨论】:

    标签: php wordpress widget


    【解决方案1】:

    我不明白你想要实现什么,但要构建一个小部件,请关注Widget API

    要获取特定小部件“选项”/属性的值,您必须使用$instance。例如,您正在尝试获取小部件的标题:

    $instance['title']
    

    如果您想在表单函数中获取小部件的标题,请使用:

     /**
     * Generates the administration form for the widget.
     *
     * @param array instance The array of keys and values for the widget.
     */
    public function form( $instance ) {
        $title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'New title', 'text_domain' );
    );
    

    你正在做的是用一些参数覆盖$instance

    要了解更多信息,请考虑做一些教程:

    1: How To Build WordPress Widgets Like A Pro

    2: How to Create a Custom WordPress Widget

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-14
      • 2012-09-14
      • 2013-03-01
      • 1970-01-01
      • 2014-02-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多