2016-07-16 14 views
-1

PHPコードを書いています。つまり、構文解析エラーが続いています。つまり、 予期しない '?>' expecting function()です。私はすべてのことを正しいようにしているようです。問題は非常に基本的な性質で、おそらくタグにマッチしています。私はコードを何回も見てきましたが、問題がどこにあるのか分かりませんでした。感謝。おかげさまで 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

前に、これらの二つの開閉

PHPタグ

(?> , <?php), 

を削除しますか?関数?変数?定数? –

+0

なぜその終了タグはありますか?新しい行があり、PHPを再び開くだけですか?コードを適切かつ一貫してインデントしてみると、これらのエラーを見やすくなります。 –

+0

@Barmar - はい、私はそれが私のコメントを変更したことに気付きました。 :) –

答えて

1

クラス内の外部関数?>は使用できません。 ?>と次の<?phpの間のすべてがエコーされるはずなので、実行可能文が許可されている場合にのみ使用できます。 echoステートメントをクラス定義に入れることはできないため、?>で出力モードに戻ることはできません。

?>とそれに続く<?phpを削除してください。

0

だけis_pageれるものを形成