1

我正在开发一个自定义的 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");') );
4

2 回答 2

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"; 
} 
于 2012-08-26T00:53:55.007 回答
0

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

于 2016-10-30T08:26:00.990 回答