WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] edit have_posts code in index.php file

[Resolved] edit have_posts code in index.php file

  • within my index.php file, i have the following code which makes the new postings appear:

    <?php if (have_posts()) : ?><?php while (have_posts()) : the_post(); ?>
    
    <div class="post">
    <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
    <div class="contenttext">
    <?php the_content(''); ?>
    </div>
    
    </div>
    
    <?php endwhile; ?>

    how do i edit this so that ONLY the most recent post appears???

    please advise. thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Not tested…

    <?php if (function_exists('get_recent_comments')) { ?>
    <li><h2><?php _e('Recent Comments'); ?></h2>
    <ul><?php get_recent_comments(); ?></ul>
    </li>
    <?php } ?>
    Moderator Michael

    @alchymyth

    use query_posts() with the parameter ‘posts_per_page=1’ before the have_posts():
    http://codex.wordpress.org/Template_Tags/query_posts

    (the rest of the code is for pagination purposes)

    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts($query_string.'&posts_per_page=1&paged='.$paged); ?>
    
    <?php if (have_posts()) : ?><?php while (have_posts()) : the_post(); ?>
    
    ...

    thanks. that works!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Resolved] edit have_posts code in index.php file’ is closed to new replies.