2013-03-27 86 views
0

這是WordPress主題的index.php我的PHP代碼:解析錯誤:語法錯誤,意外的文件結束,但沒有錯誤

<section id="content"> 
    <?php 
     $i=1; 
     while(have_posts() && i < 7):the_post(); 
     $tumbUrl = ''; 
     if(has_post_thumbnail()) 
     { 
      $tumbUrl = wp_get_attachment_url(get_post_thumbnail_id($post->ID)); 
     } 
     if(i < 4): 
    ?> 
    <div id="row1"> 
     <div id="tile<?echo $i?>" class="tile"> 
      <div id="img<?echo $i?>" class="tileimage"<?if($tumbUrl != ''):?> style="background-image:<?echo $tumbUrl; ?>"<?endif;?>></div> 
      <div id="text<?echo $i?>" class="tiletext"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></div> 
     </div> 
    </div> 
    <? 
     else: 
    ?> 
    <div id="row2"> 
     <div id="tile<?echo $i?>" class="tile"> 
      <div id="img<?echo $i?>" class="tileimage"<?if($tumbUrl != ''):?> style="background-image:<?echo $tumbUrl; ?>"<?endif;?>></div> 
      <div id="text<?echo $i?>" class="tiletext"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></div> 
     </div> 
    </div> 
    <? 
     endif; 
     endwhile; 
    ?> 
</section> 

,當我要運行它,我得到的錯誤說,Parse error: syntax error, unexpected end of file in C:\wamp\www\wordpress\wp-content\themes\Odatis\index.php on line 31,但我無法找到任何錯誤。

任何人都可以幫助我嗎?

(我的PHP版本是5.4.3)

回答

13

它很簡單。您使用短打開標籤<?

在您的php.ini中啓用短打開標籤,或者在更新的PHP版本中使用完整的php標籤,如<?php,其默認禁用。但是,如果您分享代碼,則不應在項目中使用可能導致問題的簡短語法。

http://www.php.net/manual/en/ini.core.php#ini.short-open-tag

+0

'short_open_tag = ON'但錯誤是一樣的! (它是'關',我把它改爲'開') – 2013-03-27 11:19:43

+0

「在較新的PHP版本其默認禁用」 - 什麼? – Shoe 2013-03-27 11:21:10

+0

通常你的php.ini中有多個值。搜索第二個值。 – Stony 2013-03-27 11:21:55

1

嘗試與<?= ### ?>替換所有<?echo ###?>。或者,如果您的PHP是< 5.4,則需要在php.ini中啓用短打開的標記。

噢NO:有一個分號<?php the_permalink() ?>

其中一個應該修復它缺少的,否則對不起:/

+0

我的php是5.4.3 – 2013-03-27 11:21:39

+0

您是否看到我的編輯?在'the_permalink()'處丟失了2個分號 – 2013-03-27 11:35:18

2

下面是修改工作代碼...

 
<section id="content"> 
    <?php 
     $i=1; 
     while(have_posts() && i < 7):the_post(); 
     $tumbUrl = ''; 
     if(has_post_thumbnail()) 
     { 
      $tumbUrl = wp_get_attachment_url(get_post_thumbnail_id($post->ID)); 
     } 
     if(i < 4): 
    ?> 
    <div id="row1"> 
     <div id="tile<?php echo $i; ?>" class="tile"> 
      <div id="img<?php echo $i; ?>" class="tileimage"<?php if($tumbUrl != ''): ?> style="background-image:<?php echo $tumbUrl; ?>"<?php endif; ?>></div> 
      <div id="text<?php echo $i; ?>" class="tiletext"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></div> 
     </div> 
    </div> 
    <?php 
     else: 
    ?> 
    <div id="row2"> 
     <div id="tile<?php echo $i; ?>" class="tile"> 
      <div id="img<?php echo $i; ?>" class="tileimage"<?php if($tumbUrl != ''): ?> style="background-image:<?php echo $tumbUrl; ?>"<?php endif; ?>></div> 
      <div id="text<?php echo $i; ?>" class="tiletext"><a href="<?php the_permalink(); ?>" rel="bookmark" title="a<?php the_title(); ?>"><?php the_title(); ?></a></div> 
     </div> 
    </div> 
    <?php 
     endif; 
     endwhile; 
    ?> 
</section> 
Note: Make sure the ending mark(;) are there and also the space as required.
相關問題