Here is a short Wordpress troubleshooting tutorial for theme tweaking.
A theme I’m working on runs fine and was tested on PHP 4.3.x locally in my machine, I tried testing on PHP 5.2.x and I immediately encountered syntax error on Wordpress. Traced the problem to be inside the Loop in Wordpress. Sharing it here for those having hard time making their themes work on their host.
If you are getting these PHP Token errors like the following:
Parse error: syntax error, unexpected T_ENDWHILE
Parse error: syntax error, unexpected T_ELSE
Parse error: syntax error, unexpected T_ENDIF
Solution: check your if/else or while scripts and modify them accordingly to confirm to Wordpress or your initial PHP tags.
Example script courtesy of undersigned.net, This code changes every other post to their own css tags. This runs fine and tested on PHP 4.3.x only
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$loopcounter++; ?>
<div class=“post”>
<?php if ($loopcounter % 2 == 0) { ?>
<h1 class=“even”><?php the_title(); ?></h1>
<?php } else { ?>
<h1 class=“odd”><?php the_title(); ?></h1>
<? } ?>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
You used the if/else normal php codes inside the Wordpress loop tag. Make a strict code by simply changing your if/else to Wordpress tags to make it work on PHP 5.2.x.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$loopcounter++; ?>
<div class=“post”>
<?php if ($loopcounter % 2 == 0) : ?>
<h1 class=“even”><?php the_title(); ?></h1>
<?php else : ?>
<h1 class=“odd”><?php the_title(); ?></h1>
<? endif; ?>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
That’s it, issue fixed! It should be similar to other scripts using similar coding style and not just to Wordpress. Happy theme tweaking!


September 3rd, 2008 at 10:37 am
[...] Read the rest here: Wordpress parse error: syntax error on PHP 5.2.x [...]