Forums

Can't tell if there's something wrong with this code (5 posts)

  1. estevancarlos
    Member
    Posted 2 years ago #

    It's code I've acquired from the new WordPress 2.7 Cookbook:

    <?php
    	query_posts('category_name=News');
    	if (have_posts()) : while (have_posts()) : the_post(); ?>
    				<h3><a href="<?php the_permalink() ?>" rel="bookmark" ><?php the_title(); ?></a></h3>
    			the_excerpt();
    		endwhile; else:
    	endif;
    
    wp_reset_query();
    
    ?>

    Does this need to be placed in something else?

  2. estevancarlos
    Member
    Posted 2 years ago #

    I had a more obvious error in that code that is now fixed but still not working. Thanks in advance for any assistance. I want to know if this is an error in the book as well or if I'm just doing it wrong.

  3. banago
    Member
    Posted 2 years ago #

    There are several unopened and unclosed PHP tags as I can see. Try this revised version:

    <?php query_posts('category_name=News');
    	if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    <h3><a href="<?php the_permalink() ?>" rel="bookmark" ><?php the_title(); ?></a></h3>
    	<?php the_excerpt(); ?>
    
    	<?php endwhile; else: ?>
    	<?php endif;?>
    
    <?php wp_reset_query();?>
  4. Mark / t31os
    Moderator
    Posted 2 years ago #

    No it's only missing one opening PHP tag, the others you've added above are optional...

    There should be an opening PHP tag before..

    the_excerpt();

    like..

    <?php the_excerpt();

    There's no need to switch in and out all the time, but for what it's worth it usually(well sometimes) makes the code easier to read and understand..

  5. banago
    Member
    Posted 2 years ago #

    Yes, I agree, tr10os. Thanks for pointing that out.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags