2016-07-16 192 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), 

只是形式