2013-03-27 162 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.
相关问题