2016-07-16 156 views
-1

我正在写php代码。我一直在解析错误,例如 意外'?>'期待功能()。我似乎做的都是正确的。这个问题是一个非常基本的性质,可能是关于匹配标签。我经历了许多次的代码,但无法找到问题出在哪里。请为这个微不足道的问题。任何帮助将赞赏。 谢谢。PHP代码解析错误

<?php 
class My_Widget extends WP_Widget { 

    function widget($args, $instance) { 
     // output the content of widget 
     extract($args); 
     echo $before_width; 
     echo $before_title. 'In this section'. $after_title; 

     //run query if on the page 
     if ((is_page)) { 
      $ancestor = check_page_tree(); 
      // arguments for the children of ancestors. 
      $args = array('child_of' => $ancestor, 
          'depth' => $instance['depth'], 
          'title_li' => ''); 
      // set value for get_pages to check if it's empty 
      $list_pages = get_pages($args); 

      //check if $list_pages has value. 
      if($list_pages) { 

       // open a list with the ancestor page at the top 
       ?> 
       <ul class="page_tree"> 

       //list ancestor page 
       <li class="ancestor"> 

       <a href="<?php echo get_permalink($ancestor); ?>"> 
       <?php echo get_the_title($ancestor);?></a>    
       </li> 
       <?php 

       //use wp_list_pages to list subpages of ancestor or page 
       wp_list_pages($args); 

       ?> 

       </ul> 

       <?php 
      } 

     } 

    } 
    ?>  <---------- here is the error unexpected ?> ------|  

    <?php 

    function form($instance) { 
     $defaults = array(
          'depth' => '-1' 
         ); 
     $depth = $instance[ 'depth' ]; 
     // markup for form ?> 


     <p> 
     <label for="<?php echo $this->get_field_id('depth'); ?>"> 
     Depth of list: </label> 
     <input class = "widefat" type = "text" 
     id="<?php echo $this->get_field_id('depth'); ?>" 
      name="<?php echo $this->get_field_name('depth'); ?>" value=" 
     <?php echo esc_attr($depth) ; ?>" /> 
                    </p> 
     <?php 
    } 
    // end of class 
} 
?> 
+0

什么是is_page过吗?功能?变量?不变? –

+0

为什么要关闭标签?你只是有一个新的行,然后再次打开PHP?尝试正确和一致地缩进您的代码,并且更容易看到这些错误。 –

+0

@Barmar - 是的,我意识到这一点,这就是为什么我改变了我的评论。 :) –

回答

1

您不能在类的外部函数中使用?>。它只能用于可执行语句被允许的地方,因为?>和下一个<?php之间的所有内容都应该被回显。由于您不能将echo语句放入类定义中,因此您无法切换回?>的输出模式。

所以刚刚摆脱那?>和以下<?php

0

删除这两个关闭和开启

php标记

(?> , <?php), 

只是形式