• I’d like a sidebar widget that lists the posts OLDER than the one being viewed.

    So, if the user is reading the oldest post, there would be nothing listed.

    If they’re reading the 2nd oldest, the sidebar list would show the oldest.

    If they’re reading the 3rd oldest, the sidebar list would include both the 2nd and 1st posts, etc.

    I’ve searched but can’t find anything that does this. Any clues, help, solutions?

    Thanks in advance.

Viewing 15 replies - 1 through 15 (of 18 total)
  • It shouldn’t be that hard to modify the default Recent Posts Widget to do this. Are you comfortable with PHP?

    Thread Starter sashen

    (@sashen)

    Not comfortable enough to write the code, but comfortable enough to edit files if someone tells me what to do

    Look in wp-includes/default-widgets.php, find the recent posts widgets, copy it, and paste it into your functions.php. Now rename it. class WP_Widget_Recent_Posts extends WP_Widget { becomes something like class my_only_older_Posts extends WP_Widget {. Also rename the function whose name is identical to the class name. You should probably rename some of the stuff related to caching. Read up on the widget API. Now find the $r = new WP_Query... line. Now digest some query_posts, especially this part. There will be a quiz tomorrow.

    Thread Starter sashen

    (@sashen)

    Apljdi, I’m going to fail the test.

    I’m fine up until renaming the function to the class name… but after that I’m out of my league.

    Ok. That was a bit mean on my part. 🙂

    Find the else if ( $number > 15 ) $number = 15; line. Delete everything after that and up to but not including $cache[$args['widget_id']] = ob_get_flush();. Replace that with:

    global $wpdb;
        if (is_single()) global $post;
        else {
          global $posts;
          $post = array_pop($posts);
        }
        $r = $wpdb->get_results("SELECT SQL_CALC_FOUND_ROWS ".$wpdb->posts.".* FROM ".$wpdb->posts." WHERE 1=1 AND ".$wpdb->posts.".post_type = 'post' AND (".$wpdb->posts.".post_status = 'publish') AND ".$wpdb->posts.".post_date < '$post->post_date' ORDER BY ".$wpdb->posts.".post_date DESC LIMIT 0, $number");
        if ($r) {
          echo $before_widget;
          if ( $title ) echo $before_title . $title . $after_title;
          echo '<ul>';
          foreach ($r as $v) {
            echo '<li><a href="',get_permalink($v->ID),'" title="';
            echo esc_attr($v->post_title ? $v->post_title : $v->ID);
            echo '">';
            if ( $v->post_title ) echo $v->post_title;
            else echo $v->ID;
            echo '</a></li>';
          }
          echo '</ul>'

    It seems to work on my end, but it is minimally tested. There could be bugs. Make sure to rename the cache options or your may get weird results.

    Thread Starter sashen

    (@sashen)

    Harrumph.

    I must *not* have gotten the 1st part right even, since putting in the code you wrote gives me various syntax errors.

    Any chance you could put the requisite file(s) somewhere I could download them?

    Thanks.

    Arggh… there is a syntax error. A bit of my code got lost. Try this instead.

    global $wpdb;
        if (is_single()) global $post;
        else {
          global $posts;
          $post = array_pop($posts);
        }
        $r = $wpdb->get_results("SELECT SQL_CALC_FOUND_ROWS ".$wpdb->posts.".* FROM ".$wpdb->posts." WHERE 1=1 AND ".$wpdb->posts.".post_type = 'post' AND (".$wpdb->posts.".post_status = 'publish') AND ".$wpdb->posts.".post_date < '$post->post_date' ORDER BY ".$wpdb->posts.".post_date DESC LIMIT 0, $number");
        if ($r) {
          echo $before_widget;
          if ( $title ) echo $before_title . $title . $after_title;
          echo '<ul>';
          foreach ($r as $v) {
            echo '<li><a href="',get_permalink($v->ID),'" title="';
            echo esc_attr($v->post_title ? $v->post_title : $v->ID);
            echo '">';
            if ( $v->post_title ) echo $v->post_title;
            else echo $v->ID;
            echo '</a></li>';
          }
          echo '</ul>';
          echo $after_widget;
          wp_reset_query();  // Restore global post data stomped by the_post().
        }

    If it doesn’t work I’ll put the entire working widget in the pastebin.

    Thread Starter sashen

    (@sashen)

    Yeah, I still have *something* wrong that, clearly, isn’t from the code you provided, but from my putting the widget code into functions.php incorrectly somehow.

    …wrong that, clearly, isn’t from the code you provided…

    Not as clear as you might think :). I’ll post the whole thing somewhere later today.

    Thread Starter sashen

    (@sashen)

    THANKS… looking forward to it

    Ok. I put it here. Caveat emptor: minimally tested and I could have missed some details.

    Thread Starter sashen

    (@sashen)

    Okay, have I lost my mind?

    I just copy that code into functions.php, yes?

    Because, if so, when I do, I get this error:

    Fatal error: Class 'WP_Widget' not found in /home/amc/public_html/blog/wp-includes/functions.php on line 3335

    Line 3335 is:

    class YSZineCMSOlderPosts extends WP_Widget {

    What version of WP are you running? The WP_Widget class is core code but its pretty new (and much much better than old method).

    Thread Starter sashen

    (@sashen)

    2.8.4

    You need to put the function in wp-content/themes/<yourthemename>/functions.php not in wp-includes/functions.php.

Viewing 15 replies - 1 through 15 (of 18 total)
  • The topic ‘Show only older posts sidebar widget?’ is closed to new replies.