• Resolved greg9885

    (@greg9885)


    As the title says, I’m trying to display the 5 most recent posts at the beginning of index.php. The reason for this is I’m trying to implement an image carousel, therefore I only need the 5 most recent posts to be displayed on the front page of my site.

    I have tried querying 5 most recent posts, but that seems to display them at the top of EVERY page.

    I’m sure there is some sort of option that says if the page is home, then display 5 most recent posts but I can’t quite figure it out. Any help would be greatly appreciated (and no, I prefer not to use the static front page option in WP Admin settings).

    Thanks!

Viewing 15 replies - 1 through 15 (of 20 total)
  • Look at Conditional Tags using is_home() and/or is_front_page().

    I am not sure if this will work at the top of my head however, I am guessing that you could try the following. I have not tested the code, it is written in just a few seconds but try it. You never know what might happen.

    $AreWeHome = is_home();
    
    if ( $AreWeHome == "yes" ) {
    	<em>The loop code</em>
    }
    else
    {
             <em>What you want to display if it isn't the home page</em>
    };

    Nevermind my last post. Try

    if (is_home())
    {
         Loop code here
    }
    Thread Starter greg9885

    (@greg9885)

    @michaelh I have looked through the conditional tags and I’m not really well versed enough in php to really understand how to get the 5 most recent post if home

    @snat This seems like it won’t show the rest of the posts as well with the 5 most recent??
    To clarify a little I’d like the home page like this:
    – 5 Most Recent Posts (this is the image carousel)
    then
    – Start of all the posts (including the 5 most recent)

    Thread Starter greg9885

    (@greg9885)

    @snat I never saw your second post, but I believe I have already tried doing this and it wasn’t letting me query posts with that particular code…

    Thread Starter greg9885

    (@greg9885)

    So nothing seems like it’s working for me still. If anyone could help, that would be awesome!! Here’s the code that I have…

    <?php if (is_home()) : ?>
    <?php if (have_posts()) : ?>
    <?php query_posts("showposts=5"); // show one latest post only ?>
    <?php while (have_posts()) : the_post(); ?>
    
    <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
    
    <?php endwhile; ?>
    <?php endif; ?>
    <?php endif; ?>
    Thread Starter greg9885

    (@greg9885)

    Ok, so I understand that I have to use multiple loops so I tried doing a little more research but I still seem to have 2 problems:
    1. The pagination going from page to page doesn’t work
    2. I can’t get the is_home function to work

    Just to try to clarify again on what I’m trying to do:
    I would like to basically keep how WP displays the posts (in chronological order starting with the newest at top then oldest at bottom). The only thing I would like to be different is for the 5 most recent posts to be displayed AGAIN at the top of ONLY the home page.

    Here is the code that I have so far, I’m pretty sure it’s kind of wrong.

    <?php if (have_posts()) : ?>
    <?php query_posts("showposts=5"); ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php the_title(); ?>
    <?php endwhile; ?>
    
    <?php while (have_posts()) : the_post(); ?>
    <?php the_title(); ?>
    <?php endwhile; endif; ?>

    Help would be extremely appreciated, thanks in advance πŸ™‚

    Using the WordPress Default theme’s index.php as an example, just add one of this before the line <?php if (have_posts()) : ?>

    <?php
    $recentposts=get_posts('showposts=5');
    if ($recentposts) {
    foreach($recentposts as $post) {
    setup_postdata($post);
    ?>
    <p><?php the_time('m.d.y') ?> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
    <?php
    }
    }
    ?>
    Thread Starter greg9885

    (@greg9885)

    This is what I need, now the only thing left if just to get it to work only on the home page. This is the code I’ve used, but it doesn’t seem to work…

    <?php if ( is_home() ) ;?>
    <?php
    $recentposts=get_posts('showposts=5');
    if ($recentposts) {
    foreach($recentposts as $post) {
    setup_postdata($post);
    ?>
    <p><?php the_time('m.d.y') ?> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
    <?php
    }
    }
    ?>
    <?php endif; ?>

    Link to see site please.

    Thread Starter greg9885

    (@greg9885)

    I’m working from a local server so I can’t provide a link :(. Is putting a is_home php tag the wrong way to go about this???

    Well assuming not using static front page…

    <?php
    if ( is_home() ) {
    $recentposts=get_posts('showposts=5');
    if ($recentposts) {
    foreach($recentposts as $post) {
    setup_postdata($post);
    ?>
    <p><?php the_time('m.d.y') ?> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
    <?php
    }
    }
    }
    ?>
    Thread Starter greg9885

    (@greg9885)

    No, I’m not using a static front page… this code still doesn’t seem to work for me though…

    So if you view say a category archive it is showing those 5 posts?

Viewing 15 replies - 1 through 15 (of 20 total)
  • The topic ‘Display 5 Most Recent Posts on Top of Index.php’ is closed to new replies.