Forum Moderators: coopster

Message Too Old, No Replies

unexpected T ENDWHILE ?

         

steww

10:45 am on Aug 9, 2010 (gmt 0)

10+ Year Member



Hi there, I have a piece of code which is giving me an error, anyone have any ideas?:

Parse error: syntax error, unexpected T_ENDWHILE

and it is reffering to this line "<?php endwhile; ?>"

My code is below:

----------------------------------------
<div class="homebox">
<?php $recent = new WP_Query("cat=3&showposts=1"); while($recent->have_posts()) : $recent->the_post();?>

<?php
$valid = yes;
?>

<?php
if ( $valid == yes ) {

get_the_image( array( 'custom_key' => array( 'Thumbnail', 'thumbnail' ), 'default_size' => 'thumbnail' ) ); ?>

<b><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></b>

<?php the_content_limit(60, ""); ?>

} ?>

<div style="border-bottom:1px dotted #C0C0C0; margin-bottom:10px; padding:0px 0px 10px 0px; clear:both;"></div>

<?php endwhile; ?>

</div>
------------------------------------------

Not sure why it would be questioning the endwhile?

Many Thanks

morehawes

11:07 am on Aug 9, 2010 (gmt 0)

10+ Year Member



<?php the_content_limit(60, ""); ?> 

} ?>


This is the problem. Change this to :

<?php the_content_limit(60, ""); 

} ?>

Matthew1980

11:10 am on Aug 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there steww,

The parser isn't questioning that, it's this:-

<b><a href="<?php echo the_permalink(); ?>" rel="bookmark"><?php echo the_title(); ?></a></b>

Try not to structure your code like this, it's very confusing, you only need to dip in and out whilst looping or for'ing or 'foreaching - it's not mandatory but if you want to you can, there has been a lot of debate about this recently too, but at the end of the day, your the programmer, it's your choice, we can only advise you ;)

Generally a while is structured like this:-

while(conditions){//open while

}//close while

I've not seen it done like yours before, but then again, it may be perfectly vaid syntax FAIK!

Hope you see what I mean.

[EDIT]: And as morehawes points out, doing it this way will confuse you & others trying to assess your code, hopefully you see what we are pointing to now ;-p

Cheers,
MRb

morehawes

11:20 am on Aug 9, 2010 (gmt 0)

10+ Year Member



The parser isn't questioning that, it's this:- <?php echo the_permalink(); ?>


Actually that is correct for Wordpress - there are 2 versions of these type of functions i.e. : the_permalink() and get_the_permalink() the first echo's from within the function and the second returns the result.

I do agree with MRb though, this kind of syntax switching can be a real headache and I think it was this mixing that caused the problem. My fix above should fix the issue but consider re-thinking your syntax.

I try not to mix them as much as possible and with Wordpress I tend to use the colon syntax <?php while : ?> inside my theme files and normal PHP syntax inside my functions.php

Matthew1980

11:41 am on Aug 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,
>>Actually that is correct for Wordpress

Ah. I didn't know about that as I don't use word press. They say you learn something new every day :)

Cheers,
MRb

steww

3:27 pm on Aug 9, 2010 (gmt 0)

10+ Year Member



Hi, thanks loads guys, this has worked. I used the code:

---------------------------
<?php the_content_limit(60, ""); ?>

} ?>

This is the problem. Change this to :

<?php the_content_limit(60, "");

} ?>
----------------------------

Quite a bit to take in but think Im getting there!
:-)