首页 文章

Wordpress - 使用多个字段创建多个小部件

提问于
浏览
1

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

我创建了一个,只需要一个不同的 Headers 字段 . 但是,当我尝试创建一个允许我拥有多个字段的小部件时,我现在陷入困境 . 我需要以下字段用于窗口小部件:

  • Headers

  • 简介文字

  • 正文

  • 电子邮件地址

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

我有以下代码,用于我之前的小部件(除了 Headers 之外没有额外的字段)

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' );

这很好但是 how can I add extra fields to this?

我已经尝试复制 $title 的所有实例并再次使用 $text_one 但是我不明白这个字段是如何注册的 . 有人可以帮忙吗?

1 回答

  • 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 ); ?>" />
                

    <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 ); ?>" />

    <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 }

    此处的整个代码段可以直接弹出到您的项目中 . http://pastebin.com/X7wXmcds

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

相关问题