2016-04-21 39 views
0

我正在构建一个可用于在前端显示引号的Wordpress小部件。引号可以插入作者姓名和引用文字。在Wordpress Widget中创建多维数组

我的窗口小部件文件看起来像这样:

class random_quotes extends WP_Widget { 

    function random_quotes(){ 
     $widget_ops = array(
      'classname' => 'random-quotes', 
      'description' => __('Fill in quotes to display random on your site with every new pageload.', 'onm_textdomain'), 
     ); 
     $control_ops = array(
      'id_base' => 'random-quotes', 
     ); 
     $this->WP_Widget('random-quotes', __('Random Quotes', 'onm_textdomain'), $widget_ops, $control_ops); 
    } 

    function widget($args, $instance){ 
     extract($args); 
     $title = apply_filters('widget_title', $instance['title']); 
     $quote = isset($instance['quote'])?$instance['quote']:''; 

     if($title){ 
      echo $before_title.$title.$after_title; 
     } 

     echo $before_widget;   

     ?> 
     <div class='random_quotes_widget'> 
      <div class="quote"> 
       <blockquote> 
        <?php function get_random_quote($quotes) { 
         $quote = $quotes; 
         $random_quote = array_rand($quote); 
         $random = $quote[$random_quote]; 
         return '<p>' . $random["quote"] . '.</p>'.'<cite>' . $random["name"] . '</cite>'; 
        } 
        echo get_random_quote($quotes); ?> 
       </blockquote> 
      </div> 
     </div> 
     <?php 
     echo $after_widget; 
    } 

    function update($new_instance, $old_instance) { 
     $instance   = $old_instance; 
     $instance['title'] = esc_html($new_instance['title']); 
     $instance['quotes'] = array(); 

     if (isset ($new_instance['quotes'])) { 
      foreach ($new_instance['quotes'] as $value_quote) { 
       if ('' !== trim($value_quote)) 
        $instance['quotes'][] = $value_quote; 
      } 
     } 

     return $instance; 
    } 

    function form($instance){ 
     $defaults = array('title' => __('Quotes', 'onm_textdomain')); 
     $instance = wp_parse_args((array) $instance, $defaults); 

     $quotes = isset ($instance['quotes']) ? $instance['quotes'] : array(); 
     $quotes[ count($quotes) + 1 ] = ''; 
     $quotes_html = array(); 
     $quotes_counter = 0; 

     foreach ($quotes as $quote => $value_quote) { 
      $quotes_html[] = sprintf(
       '<tr id="postcustomstuff"> 
        <td> 
         <table id="single-quote" width="100"> 
          <thead> 
           <tr> 
            <th style="line-height: 28px;">'.__('Quote', 'onm_textdomain').'<a class="button remove-row right" href="#">-</a></th> 
           </tr> 
          </thead> 
          <tbody> 
           <td> 
           <input type="text" name="%1$s[%2$s]" value="%3$s" class="widefat" placeholder="'.__('Name', 'onm_textdomain').'" /> 
           <textarea class="widefat" rows="5" cols="20" name="%1$s[%2$s]" placeholder="'.__('Quote', 'onm_textdomain').'" />%3$s</textarea> 
          </td> 
          </tbody> 
         </table> 
        </td> 
       </tr>', 
       $this->get_field_name('quotes'), 
       $quotes_counter, 
       esc_attr($value_quote) 
      ); 
      $quotes_counter += 1; 
     } 
     ?> 
     <p> 
      <label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php _e('Title:', 'onm_textdomain');?></label> 
      <input type="text" class="widefat" id="<?php echo esc_attr($this->get_field_id('title')); ?>" name="<?php echo esc_attr($this->get_field_name('title')); ?>" value="<?php echo esc_attr($instance['title']); ?>" /> 
     </p> 

     <table id="publication-downloadset-one" width="100%"> 
      <tbody> 
       <?php print join($quotes_html); ?> 
       <tr class="add-row"> 
        <td> 
         <p><input type="submit" name="savewidget" class="button widget-control-save" value="+"></p> 
        </td> 
       </tr> 
      </tbody> 

    </table> 

    <script type="text/javascript"> 
    jQuery(document).ready(function($){ 
     $('.widget').click(function() { 
      if (!$(this).hasClass('open')) { 
       $(this).css({"z-index": "100", "margin-left": "-146px"}); 
      } 
     });  

     $('.remove-row').on('click', function() { 
      $(this).parents('tr').remove(); 
      return false; 
     }); 

     $('#single-quote thead').click(function() { 
      $(this).next('tbody').toggleClass('hidden'); 
     }); 

    }); 
    </script> 

    <?php 
    } 
} 

function random_quotes_widget(){ 
    register_widget('random_quotes'); 
} 

add_action('widgets_init', 'random_quotes_widget'); 

基于@toscho对this question答案我已经找到一种方法来存储我的小部件字段中的数据作为数组。所以,感谢他的回答,我得到以下阵列输出:

Array ( 
    [0] => Quote text 1 
    [1] => Quote text 2 
    [3] => Quote text 3 
) 

但是我正在寻找的是以下的输出:

Array ( 
    [0] => Array ( 
     [quote] => Quote text 1 
     [author] => Author of the quote 1 
    ) 
    [1] => Array ( 
     [quote] => Quote tekst 2 
     [author] => Author of the quote 2 
    ) 
) 

有没有一种方法来创建这个多维数组?感谢您的帮助家伙!

回答

0

您可能需要调整它,使其正常工作,但这样的事情可以工作:

if (isset ($new_instance['quotes'])) { 
     $index = 0; 
     foreach ($new_instance['quotes'] as $value_quote) { 
      if ('' !== trim($value_quote)) { 
       $instance['quotes'][ $index ] = array(); 
       $instance['quotes'][ $index ]['quote'] = $value_quote; 
       $instance['quotes'][ $index ]['author'] = 'name author'; 
       $index++; 
      } 
     } 
    } 
+0

感谢@RST,这似乎是合法的。但是你知道我可以如何在我的小部件中填充和显示这些数组吗? – Nick

+0

这段代码应该替换'update'函数中的'if'-loop – RST

+0

Thanks @RST,以及如何在foreach循环中输出这些代码?我努力在我的网站上获取价值。 – Nick