WordPress.org

Ready to get started?Download WordPress

Forums

Show 3 previous posts into a single post page (4 posts)

  1. g10r3
    Member
    Posted 3 years ago #

    Hi,

    i need to show the 3 previous posts (with title and excerpt) into the sidebar of a single post page.

    There's a way, with some code or a plugin? I wasn't able to find anything that seems related...

    Thank u.

  2. ChaMChoM
    Member
    Posted 3 years ago #

    yeah just add this code in ur sidebar

    <?php wp_get_archives('type=postbypost&limit=3'); ?>

    this should post the past 3 posts .

  3. g10r3
    Member
    Posted 3 years ago #

    thank u, but it seems that i get only the title of the post. How che i get the date and the excerpt?

    It also seems the shows always the lastest 3 post, while i need to show the previous 3 post of the single post shown in the main area of the single.php

  4. g10r3
    Member
    Posted 3 years ago #

    If think i'm on the right way.

    I've found this snippet of code from http://codex.wordpress.org/Function_Reference/WP_Query


    // Create a new filtering function that will add our where clause to the query
    function filter_where( $where = '' ) {
    // posts in the last 30 days
    $where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
    return $where;
    }
    add_filter( 'posts_where', 'filter_where' );

    $query = new WP_Query( $query_string );

    i've managed to the the time of the current post with


    global $wp_query;
    $post_current_date = $wp_query->post->post_date;

    but i don't know how to change the code above...

    i think i have to change "date('Y-m-d', strtotime('-30 days'))" with "$post_current_date", but i'm not able to get it without errors...

    for example, this one:


    function filter_where( $where = '', $post_current_date ) {
    // posts piĆ¹ vecchi dell'attuale
    $where .= " AND post_date < '" . $post_current_date . "'";
    return $where;
    }
    add_filter( 'posts_where', 'filter_where', 10, 2 );

    $previous_query = new WP_Query( $query_string . "&posts_per_page=3");

    gives "Catchable fatal error: Object of class WP_Query could not be converted to string in sidebar.php on line 21"

    Any hint? Thank u

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags