WordPress.org

Ready to get started?Download WordPress

Forums

Display latest post on home page (5 posts)

  1. tshortli
    Member
    Posted 8 years ago #

    I've looked at the documentation for "The WordPress Loop", but I can't seem to get a definitive answer for this. I'd like my home page to display my latest post as if someone had clicked a permalink to go to it, rather than listing a number of posts. Although there is a form where you can tell WordPress to display a specified number of posts on the home page, setting this to one does not help because the function is_single() still does not return true. Has anyone here tried doing this before? Thanks for any help.

  2. pizdin_dim
    Member
    Posted 8 years ago #

    There are probably other ways of doing this, but ... In your index.php for whatever theme you're using you'll see that the main loop which displays all the posts looks something like this:


    <?php while (have_posts()) : the_post(); ?>
    .....
    <?php endwhile; ?>

    Simply adding a break statement within the loop should work:


    <?php while (have_posts()) : the_post(); ?>
    .....
    <?php break; ?>
    <?php endwhile; ?>

    Just make sure the break statement is the last thing before the endwhile.

  3. tshortli
    Member
    Posted 8 years ago #

    I have actually tried this - unfortunately it also does not cause the function is_single() to evaluate true. This is important becuase I would like the comments and various other features associated with viewing a single post to be present on the page.

  4. pizdin_dim
    Member
    Posted 8 years ago #

    Have a look at this thread:

    http://wordpress.org/support/topic/35991

  5. pizdin_dim
    Member
    Posted 8 years ago #

    According to the doco at http://codex.wordpress.org/Template_Tags/get_posts, the following should work, if you put it in place of the current loop:


    <?php
    $posts = get_posts('numberposts=1');
    foreach ($posts as $post):
    start_wp();
    the_post();
    endforeach;
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags