The Support Forums will be in read-only mode for a scheduled maintenance window on 01 September 2016 14:00 UTC - 20:00 UTC. More information.

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

  1. goldmember
    Posted 6 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(''); ?>
    <?php endwhile; ?>

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

    please advise. thanks.

  2. Maiskolben
    Posted 6 years ago #

    Not tested...

    <?php if (function_exists('get_recent_comments')) { ?>
    <li><h2><?php _e('Recent Comments'); ?></h2>
    <ul><?php get_recent_comments(); ?></ul>
    <?php } ?>
  3. Michael
    Forum Moderator
    Posted 6 years ago #

    use query_posts() with the parameter 'posts_per_page=1' before the have_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
    Posted 6 years ago #

    thanks. that works!

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.