Wordpress小部件中if / else逻辑的问题

时间:2012-08-26 00:50:55

标签: php wordpress

我正在开发一个自定义的wordpress小部件,如果小部件文本字段已完成,它会显示一些社交链接:

if (!empty($fss_fb_icon) && !empty($fss_fb_link) && !empty($fss_fb_text)) {
    echo "nothing"; 
} else {
    echo "<li class='social-new'><img src='/images/social-media/16px/".$fss_fb_icon.".png' alt='".$fss_fb_text."'/><a href='".$fss_fb_link."' target='_blank'>".$fss_fb_text."</a></li>";
}

问题是这个测试似乎总是返回else行代码,无论小部件字段的状态如何。我不确定我是否在php语法中犯了错误,或者是否有一些奇怪的wordpress小部件我不知道。完整代码如下

class FSSSocialWidget extends WP_Widget
{
  function FSSSocialWidget()
  {
    $widget_ops = array('classname' => 'FSSSocialWidget', 'description' => 'Displays social media links in the footer' );
    $this->WP_Widget('FSSSocialWidget', 'FSS Social Media Footer', $widget_ops);
  }

  function form($instance)
  {
    $instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
    $title = $instance['title'];
    $fss_fb_icon = $instance['fss_fb_icon'];
    $fss_fb_link = $instance['fss_fb_link'];
    $fss_fb_text = $instance['fss_fb_text'];
?>
    <p><label for="<?php echo $this->get_field_id('title'); ?>">Title: <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" /></label></p>
    <p><label for="<?php echo $this->get_field_id('fss_fb_icon'); ?>">Facebook Icon Name: <input class="widefat" id="<?php echo $this->get_field_id('fss_fb_icon'); ?>" name="<?php echo $this->get_field_name('fss_fb_icon'); ?>" type="text" value="<?php echo attribute_escape($fss_fb_icon); ?>" /></label></p>
    <p><label for="<?php echo $this->get_field_id('fss_fb_link'); ?>">Facebook URL: <input class="widefat" id="<?php echo $this->get_field_id('fss_fb_link'); ?>" name="<?php echo $this->get_field_name('fss_fb_link'); ?>" type="text" value="<?php echo attribute_escape($fss_fb_link); ?>" /></label></p>   
    <p><label for="<?php echo $this->get_field_id('fss_fb_text'); ?>">Facebook Link Text: <input class="widefat" id="<?php echo $this->get_field_id('fss_fb_text'); ?>" name="<?php echo $this->get_field_name('fss_fb_text'); ?>" type="text" value="<?php echo attribute_escape($fss_fb_text); ?>" /></label></p> 

<?php
  }

  function update($new_instance, $old_instance)
  {
    $instance = $old_instance;
    $instance['title'] = $new_instance['title'];
    $instance['fss_fb_icon'] = $new_instance['fss_fb_icon'];
    $instance['fss_fb_link'] = $new_instance['fss_fb_link'];
    $instance['fss_fb_text'] = $new_instance['fss_fb_text'];
    return $instance;
  }

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

    /* User-selected settings. */
   $fss_fb_icon = $instance['fss_fb_icon'];
   $fss_fb_link = $instance['fss_fb_link'];
   $fss_fb_text = $instance['fss_fb_text'];

    echo $before_widget;
    $title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']);

    if (!empty($title))
      echo $before_title . $title . $after_title;;

    // WIDGET CODE GOES HERE
    echo "<ul id='footer-social-new'>";

    if (!empty($fss_fb_icon) && !empty($fss_fb_link) && !empty($fss_fb_text)) {
        echo "nothing"; 
    } else {
        echo "<li class='social-new'><img src='/images/social-media/16px/".$fss_fb_icon.".png' alt='".$fss_fb_text."'/><a href='".$fss_fb_link."' target='_blank'>".$fss_fb_text."</a></li>";
    }

    echo "</ul>";

    echo $after_widget;
  }

}
add_action( 'widgets_init', create_function('', 'return register_widget("FSSSocialWidget");') );

2 个答案:

答案 0 :(得分:4)

您缺少两个!运算符,并且if / else块的方法错误:

if (!empty($fss_fb_icon) && !empty($fss_fb_link) && !empty($fss_fb_text))) {
    echo "<li class='social-new'>...etc...";
} else {
    echo "nothing"; 
} 

答案 1 :(得分:0)

您需要将$title = empty($instance['title']) ? ' '此空白区' '删除为''。如果不是你的功能不是空的。