WordPress.org

Ready to get started?Download WordPress

Forums

Show only older posts sidebar widget? (19 posts)

  1. sashen
    Member
    Posted 4 years ago #

    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.

  2. s_ha_dum
    Member
    Posted 4 years ago #

    It shouldn't be that hard to modify the default Recent Posts Widget to do this. Are you comfortable with PHP?

  3. sashen
    Member
    Posted 4 years ago #

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

  4. s_ha_dum
    Member
    Posted 4 years ago #

    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.

  5. sashen
    Member
    Posted 4 years ago #

    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.

  6. s_ha_dum
    Member
    Posted 4 years ago #

    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.

  7. sashen
    Member
    Posted 4 years ago #

    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.

  8. s_ha_dum
    Member
    Posted 4 years ago #

    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.

  9. sashen
    Member
    Posted 4 years ago #

    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.

  10. s_ha_dum
    Member
    Posted 4 years ago #

    ...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.

  11. sashen
    Member
    Posted 4 years ago #

    THANKS... looking forward to it

  12. s_ha_dum
    Member
    Posted 4 years ago #

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

  13. sashen
    Member
    Posted 4 years ago #

    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 {

  14. s_ha_dum
    Member
    Posted 4 years ago #

    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).

  15. sashen
    Member
    Posted 4 years ago #

    2.8.4

  16. s_ha_dum
    Member
    Posted 4 years ago #

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

  17. sashen
    Member
    Posted 4 years ago #

    AHA!

    Well, now that I've done it correctly, it looks perfect. THANKS.

  18. DanielGenser
    Member
    Posted 4 years ago #

    apljdi, I'd love to see the code you put in the pastebin, but it's been deleted. Can you or sashen repost the code to the forum for the benefit of the rest of us?

    Thanks!

    Daniel

  19. mmlog
    Member
    Posted 4 years ago #

    I am also interessted in that code. Anyone out there who can provide it?

Topic Closed

This topic has been closed to new replies.

About this Topic