【问题标题】:Wordpress - creating multiple widgets with several fieldsWordpress - 创建具有多个字段的多个小部件
【发布时间】:2015-09-17 23:16:49
【问题描述】:

我正在尝试创建一些自定义小部件。

我创建了一个,它只需要一个不同的标题字段。但是,当我尝试创建一个允许我拥有多个字段的小部件时,我现在卡住了。我需要小部件的以下字段:

  • 标题
  • 介绍文字
  • 正文
  • 电子邮件地址

我希望小部件表单允许用户使用文本更新上面的字段。然后我想输出他们在我的侧边栏小部件中编写的文本。

我有以下代码用于我之前制作的小部件(除了标题之外没有其他字段)

class registercv_widget extends WP_Widget {

        function __construct() {
            parent::__construct(
                'registercv_widget',
                __('Register CV Widget', 'registercv_widget_domain'),
                array( 'description' => __( 'Provides a "Register CV" button which launches a pop-up', 'registercv_widget_domain' ), )
            );
        }

        public function widget( $args, $instance ) {
            $title = apply_filters( 'widget_title', $instance['title'] );
            echo $args['before_widget'];
            echo '<div class="widget-wrapper">';
            if ( ! empty( $title ) )
                echo $args['before_title'] . $title . $args['after_title'];

    // This is where you run the code and display the output
            echo __( '<div class="register-button"><span><div class="button white">Send your CV</div></span></div>', 'registercv_widget_domain' );
            echo '</div>';
            echo $args['after_widget'];
        }

    // Widget Backend
        public function form( $instance ) {
            if ( isset( $instance[ 'title' ] ) ) {
                $title = $instance[ 'title' ];
            }
            else {
                $title = __( 'Register as a candidate', 'registercv_widget_domain' );
            }
    // Widget admin form
            ?>
            <p>
                <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
                <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
            </p>
        <?php
        }

    // Updating widget replacing old instances with new
        public function update( $new_instance, $old_instance ) {
            $instance = array();
            $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
            return $instance;
        }
    }

    function registercv_load_widget() {
        register_widget( 'registercv_widget' );
    }
    add_action( 'widgets_init', 'registercv_load_widget' );

这很好用,但是我怎样才能添加额外的字段呢?

我尝试复制$title 的所有实例并再次创建它们,但使用$text_one 但我不明白该字段是如何注册的。有人可以帮忙吗?

【问题讨论】:

  • 你有没有得到这个答案?关于我想问的表格,我有一个类似的问题,但如果你找到了解决方案,我会支持你的问答。
  • @Darth_Vader 刚刚为您添加了我的答案,希望对您有所帮助!

标签: widget wordpress


【解决方案1】:

找到了解决办法。

我从上面的代码开始,能够像这样创建多个字段:

echo $args['before_widget'];
        echo '<div class="widget-wrapper">';
        if ( ! empty( $title ) )
            echo $args['before_title'] . $title . $args['after_title'];
        if ( ! empty( $text_two ) )
            echo '<p>' . $text_two . '</p>';
        if ( ! empty( $link_text ) )
            echo '<p><a href="/employers/executives">' . $link_text . '</a><i class="fa fa-play"></i></p>';
        echo '</div>';
        echo $args['after_widget'];

上面的示例使用了$text_two$link_text 变量,可以通过小部件屏幕使用以下内容进行设置:

public function form( $instance ) {
        if ( isset( $instance[ 'title' ] ) ) {
            $title = $instance[ 'title' ];
        }
        else {
            $title = __( 'Executive Search', 'executive_widget_domain' );
        }

        if ( isset( $instance[ 'text_two' ] ) ) {
            $text_two = $instance[ 'text_two' ];
        }
        else {
            $text_two = __( 'Systematic searching to map the right talent for Director level roles using the best possible sector intelligence.', 'executive_widget_domain' );
        }

        if ( isset( $instance[ 'link_text' ] ) ) {
            $link_text = $instance[ 'link_text' ];
        }
        else {
            $link_text = __( 'Visit Executive Search', 'executive_widget_domain' );
        }
        // Widget admin form
        ?>
        <p>
            <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
            <br /><br />
            <label for="<?php echo $this->get_field_id( 'text_two' ); ?>"><?php _e( 'Main text:' ); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id( 'text_two' ); ?>" name="<?php echo $this->get_field_name( 'text_two' ); ?>" type="text" value="<?php echo esc_attr( $text_two ); ?>" />
            <br /><br />
            <label for="<?php echo $this->get_field_id( 'link_text' ); ?>"><?php _e( 'Link text' ); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id( 'link_text' ); ?>" name="<?php echo $this->get_field_name( 'link_text' ); ?>" type="text" value="<?php echo esc_attr( $link_text ); ?>" />
        </p>
    <?php
    }

整个代码 sn-p 在这里可以直接弹出到您的项目中。 http://pastebin.com/X7wXmcds

将上述代码放在您的functions.php 中将为您提供一个名为Executive Search. It has three fields:title,text_two(a simple text string) andlink text` 的小部件。这就是用户可以添加的三个字段。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-19
    • 1970-01-01
    • 2021-07-19
    • 2016-11-05
    • 2012-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多