dousu8767 2017-02-12 16:44
浏览 50
已采纳

注意:未定义的变量:wordpress小部件中的firsthalf

Im getting this notice on two lines I tried to fix it using isset but no chance.

Im getting notices on variables $firsthalf and $secondhalf

Notice: Undefined variable: firsthalf

and

Notice: Undefined variable: firsthalf

So the code is below:

for ($i=0;$i<$catCount;$i++) {
if ($i<$catColumns){
$firsthalf = $firsthalf.''.$catArray[$i].'</li>';
}
elseif ($i>=$catColumns){
$secondhalf = $secondhalf.''.$catArray[$i].'</li>';
}  
} 

Which one is the fix for this I have hours and tried all ways but still getting the same issue.Do you have any idea about this? Thanklyou!

Edit below is all wordpress function

function wpse97413_register_custom_widgets() {
register_widget( 'wpse97411_WP_Widget_Categories' );
register_widget( 'wpse97411_WP_Search_Widget' );
}
add_action( 'widgets_init', 'wpse97413_register_custom_widgets' );

/* Widget Video Categories framework */
class wpse97411_WP_Widget_Categories extends WP_Widget {

    public function __construct() {
        $widget_ops = array( 'classname' => 'widget_categories', 'description' => __( "A list of categories." ) );
        parent::__construct('categories', __('Categories'), $widget_ops);
    }

    public function widget( $args, $instance ) {

        /** This filter is documented in wp-includes/default-widgets.php */
        $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Categories' ) : $instance['title'], $instance, $this->id_base );

        $c = ! empty( $instance['count'] ) ? '1' : '0';
        $h = ! empty( $instance['hierarchical'] ) ? '1' : '0';
        $d = ! empty( $instance['dropdown'] ) ? '1' : '0';

        echo $args['before_widget'];
        if ( $title ) {
            echo $args['before_title'] . $title . $args['after_title'];
        }

$catArray = explode("</li>",wp_list_categories('title_li=&echo=0&depth=1&show_count=0'));
$catCount = count($catArray) - 1;
$catColumns = round($catCount / 2);
for ($i=0;$i<$catCount;$i++) {
if ($i<$catColumns){
$firsthalf = $firsthalf.''.$catArray[$i].'</li>';
}
elseif ($i>=$catColumns){
$secondhalf = $secondhalf.''.$catArray[$i].'</li>';
}  
} ?>
<nav class="nav-categories">
<ul>
<?php echo $firsthalf; ?>
</ul>
<div class="hidden-content" id="hidden-categories">
<ul>
<?php echo $secondhalf; ?>
</ul>
</div>
<button class="category-toggle" data-action="content-toggle" data-target="#hidden-categories" data-more="<?php _e( 'Show More Categories', 'framework' ); ?>" data-less="<?php _e( 'Show Less Categories', 'framework' ); ?>"><?php _e( 'Show More Categories', 'framework' ); ?></button>
</nav>  
    <?php   echo $args['after_widget'];
    }

    public function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        $instance['count'] = !empty($new_instance['count']) ? 1 : 0;

        return $instance;
    }

    public function form( $instance ) {
        //Defaults
        $instance = wp_parse_args( (array) $instance, array( 'title' => '') );
        $title = esc_attr( $instance['title'] );
        $count = isset($instance['count']) ? (bool) $instance['count'] :false;
?>
        <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><br />
<?php
    }

}
  • 写回答

1条回答 默认 最新

  • douxie2029 2017-02-12 17:05
    关注

    You should initialize variable before using it:

    $firsthalf = '';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等