Series Posting in Wordpress

In my functions.php file, I have some code which implements series posting. This relies on the thematic framework [sourcecode language="php"] function childtheme_article_footer($footer) { // This variable is the stuff I put before the regular footer $extra_stuff='';

global $id, $post; $series = get_post_meta($post->ID, 'Series', true); $pstlink = get_permalink(); $psttitle = get_the_title(); $pstbody = get_the_excerpt();

// Check for a 'series' as a custom field - if there is one, grab the value contained if($series) : $args = array( 'numberposts' => -1, 'meta_key' => 'Series', 'meta_value' => $series, );

// This bit grabs the series posts, and stores the HTML linking to them in $extra_stuff variable $series_posts = get_posts($args); if($series_posts) : $class = preg_replace("/[^a-z0-9\\040\\.\\-\\_\\\\]/i", "", $series); $class = strtolower(str_replace(array(' ', ' '), '-', $class)); if (is_single()) { $extra_stuff = '<div class="series series-' . $class . '"><h4 class="series-title">' . __('Articles in this series') . '</h4><ul>'; foreach($series_posts as $serial) : if($serial->ID == $post->ID) $extra_stuff = $extra_stuff . '<li class="current-post">' . $serial->post_title . '</li>'; else $extra_stuff = $extra_stuff . '<li><a href="' . get_permalink($serial->ID) . '" class="postID_' . $serial->ID . '" title="' . str_replace('"', '"', $serial->post_title) . '">' . str_replace('"', '"', $serial->post_title) . '</a></li>'; endforeach; $extra_stuff = $extra_stuff . '</ul></div>'; } else { $extra_stuff = $extra_stuff . '<div class="series series-' . $class . '">'; $extra_stuff = $extra_stuff . '<h4 class="series-title"><a href="' . $pstlink . '">' . $psttitle . '</a>'; $extra_stuff = $extra_stuff . ' is part of a series' . '</h4></div>'; } endif; endif;

// This is irrelevant to series list, but it does add twitter links and the like $pageurl = get_bloginfo('siteurl'); $pageurl = $pageurl . "?p=" . $post->ID; $extra_stuff_post = '<div class="socialnetwork"><ul>'; $extra_stuff_post = $extra_stuff_post . '<li>Send to: <a class="twitterlink" href="http://twitter.com/home?status=Currently reading ' . $pageurl . '" title="Click to send ' . $psttitle . ' to Twitter!" target="_blank">Twitter</a></li>'; $extra_stuff_post = $extra_stuff_post . '<li><a class="deliciouslink" href="http://del.icio.us/post?url=' . $pstlink . '&title=' . $psttitle . '" title="Click to bookmark ' . $psttitle . ' on del.icio.us!" target="_blank">del.icio.us</a></li>'; $extra_stuff_post = $extra_stuff_post . '<li><a class="digglink" href="http://digg.com/submit?phase=2&url=' . $pstlink . '&title='. $psttitle . '&bodytext=' . $pstbody . '" title="Click to bookmark ' . $psttitle . ' on Digg!" target="_blank">Digg</a></li>'; $rss_url = get_bloginfo('rss2_url'); $extra_stuff_post = $extra_stuff_post . '<li>Use the: <a class="rsslink" href="' . $rss_url . '" title="Click to Subscribe to the Feed" rel="alternate" type="application/rss+xml" target="_blank">RSS Feed</a></li>'; $extra_stuff_post = $extra_stuff_post . '</ul></div>';

// This slams it all together at the end of the post $footer = $extra_stuff . $footer . $extra_stuff_post; return $footer; }

// This replaces thematics stuff with mine add_action('thematic_postfooter','childtheme_article_footer'); [/sourcecode]

It's been a while since I did this, and my aim of tidying the output is now further away than it was. What I want is for the code to ONLY output the first post in a series, the previous post, the current post (with no link), the next post and the last post. If any of these posts are one and the same, they would only be output once.

I'm really drawing a bit of a blank on how to do this - though I sense I'm missing a trick. If anyone has any bright ideas, I'd be glad to hear them. (Now Solved)

Note - comments are disabled due to this post, for some reason, attracting about a third of my spam. To comment, see the followup post above.