WordPress.org

Ready to get started?Download WordPress

Forums

Want to display latest 5 posts except current one (4 posts)

  1. jag5311
    Member
    Posted 8 years ago #

    Ok,

    I want to display on the sidebar the last 5 postings. However, if they are viewing an individual post, and that individual post HAPPENS to be in the last 5 posted, I don't want it to appear in the list.

    So basically, on the homepage, it will show the last 5 postings with nothing special going on.

    On one of the individual pages, I want the current one they are viewing stripped out.

    How can I do this for Word Press?

    I guess it might have something to do with get_posts()

    http://codex.wordpress.org/Template_Tags/get_posts

    Thanks

  2. Minna
    Member
    Posted 8 years ago #

    [Never mind, stupid me :) ]

  3. moshu
    Member
    Posted 8 years ago #

    You can also take a look at this page:
    http://codex.wordpress.org/Template_Tags/query_posts
    Additionally, try a search here fro query_posts and offset.

  4. Minna
    Member
    Posted 8 years ago #

    OK, this is very very ugly, but it works. I don't know the secrets of WP so I can't make the most of it. Maybe someone will clean this :)

    <?php if ( is_single() ) {
    if(get_query_var('p')) {
    $current = get_query_var('p');
    } else {
    $postname = get_query_var('name');
    $current = $wpdb->get_var("SELECT ID from $wpdb->posts WHERE post_name = '$postname'");
    }
    print "<ul>";
    $count = 0;
    $posts = get_posts('numberposts=6');
    foreach ($posts as $post) :
    setup_postdata($post);
    global $id;
    if ( $count < 5 and $id != $current ) { ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php $count++;
    }
    endforeach;
    print "</ul>";
    } ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.