サポート » 使い方全般 » テンプレート内の関数

  • 解決済 Kite

    (@ixkaito)


    フレームワーク Thematic を使用しておりますが、テンプレートファイル内の関数の記述について、

    <?php
    function hoge(){echo 'hoge';}
    hoge();
    ?>

    では実行されますが、

    <?php
    hoge();
    function hoge(){echo 'hoge';}
    ?>

    とした場合、エラーとなります。

    Thematicの関数を取り除いたテンプレートではどちらも問題なく動いていたと思いますが、これはThematicの関数の問題でしょうか。

    どなたかご存知の方いらっしゃいましたら、ご教授のほどよろしくお願い致します。

4件の返信を表示中 - 1 - 4件目 (全4件中)
  • こんにちは

    WordPress3.51
    VERSION: 1.0.3.2で試してみましたが、

    <?php
    hoge();
        function hoge(){echo 'hoge';}
    
    ?>ddddddd

    として、数種類のテンプレートで試してみましたが、動作するみたいです。

    どんな、エラーが出るのですか?

    トピック投稿者 Kite

    (@ixkaito)

    nobitaさん、こちらの質問にもご回答いただきましてありがとうございます。

    再度色々試してみましたところ、どうやら

    <?php
    while ( have_posts() ) : the_post();
    endwhile;
    ?>

    のループ内で下記のエラーが出ます。

    Fatal error: Call to undefined function hoge() in /〜〜.php on line ◯◯

    single.phpのwhile loopの中で、関数を使ってみましたが、私の環境では、hogeがechoされて、エラーは、発生していないようです。
    テストしたコード

    <div id="container">
    
    			<?php
    				// action hook for placing content above #content
    				thematic_abovecontent();
    
    				// filter for manipulating the element that wraps the content
    				echo apply_filters( 'thematic_open_id_content', '<div id="content">' . "\n\n" );
    
    	            // start the loop
    	            while ( have_posts() ) : the_post();
    
    				hoge();
        	        // create the navigation above the content
    				thematic_navigation_above();
    
        	        // calling the widget area 'single-top'
        	        get_sidebar('single-top');
    
        	        // action hook creating the single post
        	        thematic_singlepost();
    
        	        // calling the widget area 'single-insert'
        	        get_sidebar('single-insert');
    
        	        // create the navigation below the content
    				thematic_navigation_below();
    
           			// action hook for calling the comments_template
        	        thematic_comments_template();
    
        	        // end the loop
            		endwhile;
    
    				function hoge(){echo 'hoge';}
    
        	        // calling the widget area 'single-bottom'
        	        get_sidebar('single-bottom');
    			?>
    
    			</div><!-- #content -->
    
    			<?php
    				// action hook for placing content below #content
    				thematic_belowcontent();
    			?>
    		</div><!-- #container -->

    想像ですが、while loopの中で、関数を定義していませんか?

    fatal errorは、ループの中で関数が定義されて、二回目のループで、同じ関数が定義されているために出ているのではないかと思います。

    トピック投稿者 Kite

    (@ixkaito)

    想像ですが、while loopの中で、関数を定義していませんか?

    してました!

    fatal errorは、ループの中で関数が定義されて、二回目のループで、同じ関数が定義されているために出ているのではないかと思います。

    同じループ内でも、先に関数を定義した場合は問題ないでしょうか?

4件の返信を表示中 - 1 - 4件目 (全4件中)
  • トピック「テンプレート内の関数」には新たに返信することはできません。