为什么这个小部件没有显示其中一个图标?

时间:2015-01-13 作者:Leona grey

我已经编写了一个小部件。在管理区域,用户可以输入他们的youtube、facebook、twitter和google+的url。在浏览器中的网站前端,他们可以单击这4个图标中的任何一个,amp;这将把他们带到社交网络。facebook、twitter和;youtube正在付款,但youtube却没有。

请看一下我的代码(下面),告诉我为什么没有显示youtube徽标

 class mp_example_widget02 extends WP_widget {

    function mp_example_widget02() {
    parent::WP_widget(false, $name = \' Follow us\');
    }

    function widget($args, $instance) {
    extract( $args );

    $title = apply_filters(\'widget_title\', $instance[\'title\']);
    $address = $instance[\'address\'];
    $email =  $instance[\'email\'];
    $phone =  $instance[\'phone\'];

    echo $before_widget;

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

    if ( $address ) {
    echo  \' <div class="widget-textarea">\' . $address . \' </div> \'; 
    }

    if ( $email ) {
    echo \'<a href="mailto:\' .antispambot($email) .\'"  > \' .$email . \'</a><br />\';
      }

    if ( $phone ) {
    if ( wp_is_mobile() ) {
    echo  "<a href=\'tel:$phone\'  >$phone</a><br />";
    }else{
    echo \'
         <div class="widget-textphone">\' . $phone . \' </div> \'; 
         }
         }


    echo $after_widget;
    }

     function update($new_instance, $old_instance) {
     $instance = $old_instance;
     $instance[\'title\'] = strip_tags($new_instance[\'title\']);
     $instance[\'address\'] = strip_tags($new_instance[\'address\']);
     $instance[\'email\'] = ( is_email($new_instance[\'email\']) ) ? $new_instance[\'email\'] : \'\';
    $instance[\'phone\'] = strip_tags($new_instance[\'phone\']);



     return $instance;
     }

     function form($instance) {

     $title = esc_attr($instance[\'title\']);
     $address = esc_attr($instance[\'address\']);
     $email = esc_attr($instance[\'email\']);
     $phone = esc_attr($instance[\'phone\']);
     ?>

     <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 $title; ?> " />
     </p>
 <p>

        <label for="<?php echo $this->get_field_id(\'address\'); ?>"><?php _e(\'Address\'); ?></label>

        <textarea class="widefat" id="<?php echo $this->get_field_id(\'address\'); ?>" name="<?php echo $this->get_field_name(\'address\'); ?>"><?php echo $address; ?></textarea>

    </p> 
    <br /> <br /> <p></p>

     <p>
    <label for="<?php echo  $this->get_field_id(\'email\'); ?>">
     <?php _e(\'Email\'); ?>  </label>
     <input class="widefat" id="<?php echo $this->get_field_id(\'email\'); ?>" name="<?php echo $this->get_field_name(\'email\'); ?>" type="email" value="<?php echo $email; ?> " />
    </p>


     <p> <label for="<?php echo $this->get_field_id(\'phone\'); ?> ">
     <?php _e(\'Phone\'); ?> </label>
     <input class="widefat" id="<?php echo $this->get_field_id(\'phone\'); ?>" name="<?php echo $this->get_field_name(\'phone\'); ?>" type="phone" value="<?php echo $phone; ?>" />

        </p>

    <?php
    }
    }

    add_action(\'widgets_init\', create_function(\'\', \'return register_widget("mp_example_widget");\'));


class phone_social02 extends WP_Widget {

    function phone_social02() {
        parent::WP_Widget(false, $name = __(\'Share us\', \'phone_social02\') );
    }

function form($instance) {
if( $instance) {

     $email = esc_attr($instance[\'email\']);
     $youtube = esc_attr($instance[\'youtube\']);
     $facebook = esc_attr($instance[\'facebook\']);
     $twitter = esc_attr($instance[\'twitter\']);
     $gplus = esc_attr($instance[\'gplus\']);
     } else {
     $title = \'\';
     $telephone = \'\';
     $email = \'\';
     $youtube = \'\';
     $facebook = \'\';
     $twitter = \'\';
     $gplus = \'\';
    }
?>

<p>
</p>
<p>
<label for="<?php echo $this->get_field_id(\'youtube\'); ?>"><?php _e(\'Youtube:\', \'wp_widget_plugin\'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id(\'youtube\'); ?>" name="<?php echo $this->get_field_name(\'youtube\'); ?>" type="text" value="<?php if(empty($youtube)) { echo "http://";} else { echo $youtube; } ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id(\'facebook\'); ?>"><?php _e(\'Facebook URL:\', \'wp_widget_plugin\'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id(\'facebook\'); ?>" name="<?php echo $this->get_field_name(\'facebook\'); ?>" type="text" value="<?php if(empty($facebook)) { echo "http://";} else { echo $facebook; } ?>" />
</p>

<p>
<label for="<?php echo $this->get_field_id(\'twitter\'); ?>"><?php _e(\'Twitter URL:\', \'wp_widget_plugin\'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id(\'twitter\'); ?>" name="<?php echo $this->get_field_name(\'twitter\'); ?>" type="text" value="<?php if(empty($twitter)) { echo "http://";} else { echo $twitter; } ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id(\'gplus\'); ?>"><?php _e(\'Google Plus URL:\', \'wp_widget_plugin\'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id(\'gplus\'); ?>" name="<?php echo $this->get_field_name(\'gplus\'); ?>" type="text" value="<?php if(empty($gplus)) { echo "http://";} else { echo $gplus; } ?>" />
</p>
<?php }



function update($new_instance, $old_instance) {
      $instance = $old_instance;

      $instance[\'email\'] = strip_tags($new_instance[\'email\']);
      $instance[\'youtube\'] = strip_tags($new_instance[\'youtube\']);
      $instance[\'facebook\'] = strip_tags($new_instance[\'facebook\']);
      $instance[\'twitter\'] = strip_tags($new_instance[\'twitter\']);
      $instance[\'gplus\'] = strip_tags($new_instance[\'gplus\']);

           return $instance;
}


function widget($args, $instance) {
   extract( $args );


   $email = $instance[\'email\'];
   $facebook = $instance[\'youtube\'];
   $facebook = $instance[\'facebook\'];
   $twitter = $instance[\'twitter\'];
   $gplus = $instance[\'gplus\'];

   echo $before_widget;


      echo \'<div id="phonesocialmain">\';
      echo \'<div id="socialdetmain">\';


   echo\'<p>Follow us: </p>\';
   if( $youtube ) {
     echo \'<a href="\'.$youtube.\'" target="_blank"> <img src="\'. get_stylesheet_directory_uri() .\'/images/you3.png" height="35" width="35" alt="youtube"></a>\';
    }
    if ( $facebook ) {
   echo  \'<a href="\'.$facebook.\'" target="_blank"><img src="\'. get_stylesheet_directory_uri() .\'/images/fb3.png" height="35" width="35" alt="facebook"></a>\';  
   }
   if ( $twitter ) {
   echo  \'<a href="\'.$twitter.\'" target="_blank"><img src="\'. get_stylesheet_directory_uri ().\'/images/twit3.png" height="35" width="35" alt="Twitter"></a>\';  
   }
   if ( $gplus ) {
 echo  \'<a href="\'.$gplus.\'" target="_blank"><img src="\'. get_stylesheet_directory_uri() .\'/images/go3.png" height="35" width="35" alt="gplus"></a>\';  
   }
   echo  \'</div>\'; 

  echo \'</div>\'; 
    echo \'</div>\';
   echo $after_widget;
}
}   

add_action(\'widgets_init\', create_function(\'\', \'return register_widget("phone_social02");\'));

1 个回复
最合适的回答,由SO网友:Andrew Bartel 整理而成

你有一个$facebook = $instance[\'youtube\']; 而不是$youtube = $instance[\'youtube\'];

结束

相关推荐

Reorder dashboard widgets

我最近试图通过编写插件对仪表板小部件进行重新排序。我做到了这一点:使用以下代码:<?php /* * Plugin Name: Custom Dashboard * Description: Custom dashboard for Avare sites. * Author: Avare * Version: 1.0 */ function sort_dashboard_widgets() { $lef