WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] edit have_posts code in index.php file (4 posts)

  1. goldmember
    Member
    Posted 4 years ago #

    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.

  2. Maiskolben
    Member
    Posted 4 years ago #

    Not tested...

    <?php if (function_exists('get_recent_comments')) { ?>
    <li><h2><?php _e('Recent Comments'); ?></h2>
    <ul><?php get_recent_comments(); ?></ul>
    </li>
    <?php } ?>
  3. alchymyth
    The Sweeper & Moderator
    Posted 4 years ago #

    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(); ?>
    
    ...
  4. goldmember
    Member
    Posted 4 years ago #

    thanks. that works!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.