Series Linking - A solution

A while ago, I posted about a problem I was having with series links. I usually would post the solution soon after, but it seems I forgot. I can't recall the process of how I got to this solution, so apologies for not linking anywhere I should to give credit, but this is what I have in my functions.php now.

Things I'd like to work out are: Can I have a post in more than one series (with current code, no, I suspect a rewrite may be in order for that) Can I have a second type of series link, one to link to another series? Ideally, this would be 'previous series' and 'next series'. I suspect I wouldn't want to attach these relations to posts, as I'd want the links to be on the last and first posts in a series, and that may change with new posts being added. Can I list the names of series which I have defined on an admin page somewhere? Ideally, with the post titles listed right there alongside.

[php]function mrk_theme_article_footer($content) { $newcontent = $content; $psttitle=get_the_title();

global $id, $post;

// Check to see if this post is part of a series, if it isn't skip $series = get_post_meta($post->ID, 'Series', true); if ($series) : $args = array( 'numberposts' => -1, 'meta_key' => 'Series', 'meta_value' => $series, 'orderby' => 'post_date', 'order' => 'asc', ); $series_posts = get_posts($args);

// Check how many posts have been grabbed - if only 1, then move on $seriescount = count($series_posts); if ($seriescount > 1) { // Now to throw away all but the first, previous, current, next and last base this on date. // First let's initialise all the variables - these values will change soon! $currpostdate = $post->post_date; $currpostID = $post->ID;

$lastpostdate = $currpostdate; $lastpostID=$currpostID;

$firstpostdate = $currpostdate; $firstpostID = $currpostID;

$nextpostdate = $currpostdate; $nextpostID = $currpostID;

$prevpostdate = $currpostdate; $prevpostID = $currpostID;

$counter = 0;

foreach ($series_posts as $serial) : // Check for two key posts earlier than this - first look if I have an earlier post than the known earliest // currpost refers to the post currently selected in the loop // firstpost refers to the first post in a series - lastpost similar // prev and next refer to the next and prev post in a series // serial refer to the current series post being examined to see if it may be one of the above $counter=$counter +1; if ($serial->post_date < $firstpostdate) { $firstpostdate = $serial->post_date; $firstpostID=$serial->ID; if ($prevpostdate == $currpostdate) { // I have an earlier post than the current one, so if the previous post is set to point at the current post, change this $prevpostdate = $firstpostdate; $prevpostID = $firstpostID; } }

// Is the post found dated between the current post and the current date for previous post? if ($serial->post_date < $currpostdate) { if ($serial->post_date > $prevpostdate ) { $prevpostdate = $serial->post_date; $prevpostID = $serial->ID; } } if ($serial->post_date > $lastpostdate) { $lastpostdate = $serial->post_date; $lastpostID=$serial->ID; if ($nextpostdate == $currpostdate) { $nextpostdate = $lastpostdate; $nextpostID = $lastpostID; } } if ($serial->post_date > $currpostdate ) { if ($serial->post_date < $nextpostdate ) { $nextpostdate = $serial->post_date; $nextpostID = $serial->ID; } }

if ($serial->post_date == $currpostdate) { $postnumber = ordinal_suffix($counter,1); } endforeach; // I now know the first, prev, next and last in series

// First, we'll do some database lookups to get the info needed. I know it's already in an array, but I'm being lazy $firstpostarray=get_post($firstpostID); $firsttitle = $firstpostarray->post_title; $firstlink = get_permalink($firstpostID);

$prevpostarray=get_post($prevpostID); $prevtitle = $prevpostarray->post_title; $prevlink = get_permalink($prevpostID);

$lastpostarray=get_post($lastpostID); $lasttitle = $lastpostarray->post_title; $lastlink = get_permalink($lastpostID);

$nextpostarray=get_post($nextpostID); $nexttitle = $nextpostarray->post_title; $nextlink = get_permalink($nextpostID);

$seriesclass = preg_replace("/[^a-z0-9\\040\\.\\-\\_\\\\]/i", "", $series); $seriesclass = 'series-' . strtolower(str_replace(array(' ', ' '), '-', $seriesclass));

// Single posts are handled differently to summary posts if (is_single()) { // Create a div to hold the links $extra = '<div class="series seriessingle ' . $seriesclass . '"><h4 class="series-title">Series Information</h4>'; $extra = $extra . '<ul>';

// This bit of code is for first and last pages if ($firstpostID == $currpostID || $lastpostID == $currpostID) { // If the first or last post, show the whole list of links $extra = $extra . '<p>&quot;' . $psttitle . '&quot; is the '; if ($firstpostID == $currpostID) { $extra = $extra . 'first'; } else { $extra = $extra . 'last'; } $extra = $extra . ' in a sequence of ' . $seriescount . ' posts</p>';

// For the first and last post only show the full list foreach ($series_posts as $serial) : //Output list of posts $extra = $extra . '<li>'; if ($serial->ID == $currpostID) { $extra = $extra . '<strong>This post: </strong> ' . $psttitle; } else { $extra = $extra . '<a href="' . get_permalink($serial->ID) . '">' . $serial->post_title . '</a>'; } $extra = $extra . '</li>'; endforeach; // Finish outputting list of posts $extra = $extra .'</ul>'; $extra = $extra . '</div> <!-- end of series stuff -->'; } // The above bit was for first and last pages else { // Output a display for posts other than the first and last. $extra = $extra . '<p>&quot;' . $psttitle . '&quot; is '. $postnumber .' in a larger sequence of ' . $seriescount .' posts</p>'; $extra = $extra . '<ul>';

// Is the previous post the first post? if ($firstpostID == $prevpostID) { $extra = $extra . '<li><a href="'. $firstlink .'">'. $firsttitle. '</a> - First in Series (and Previous Post)</li>'; } else { $extra = $extra . '<li><a href="'. $firstlink .'">'. $firsttitle. '</a> - First in Series</li>'; $extra = $extra . '<li><a href="'. $prevlink .'">'. $prevtitle. '</a> - Previous Post</li>'; } // End output first and previous // Is the next post the last post? if ($nextpostID == $lastpostID) { $extra = $extra . '<li><a href="'. $nextlink .'">'. $nexttitle. '</a> - Next in Series (and Last Post)</li>'; } else { //End next post is last post $extra = $extra . '<li><a href="'. $nextlink .'">'. $nexttitle. '</a> - Next Post</li>'; $extra = $extra . '<li><a href="'. $lastlink .'">'. $lasttitle. '</a> - Last in Series</li>'; } // End output next and last $extra = $extra .'</ul>'; $extra = $extra . '</div> <!-- end of series stuff -->'; } } // End of code if it is a single post

else { //Now do summary posts $extra = '<div class="series seriessummary "' . $seriesclass .'"><p>'; if ($currpostID == $firstpostID) { // For the first post in the series, link to the latest post $extra = $extra . 'This post is the first in a series of ' . $seriescount .' posts, the latest is '; $extra = $extra . '<a href="' . $lastlink . '">' . $lasttitle . '</a></p>'; } else { // For any post in the series, link to the first post

$extra = $extra . 'This post is ';

// The latest post is a special case if ($currpostID == $lastpostID) { $extra = $extra . 'the latest in'; } else { $extra = $extra . 'part of'; } $extra = $extra . ' a series of ' . $seriescount .' posts which begins with <a href="'; $extra = $extra . $firstlink; $extra = $extra . '">' . $firsttitle . '</a>';

// If not the latest post, link to that. if ($currpostID == $lastpostID) { $extra_stuff_post = $extra_stuff_post . '. '; } else { $extra_stuff_post = $extra_stuff_post . ', the latest post in the series is <a href="'; $extra_stuff_post = $extra_stuff_post . $lastlink . '">' . $lasttitle . '</a>.'; } $extra_stuff_post = $extra_stuff_post . '</p>'; } $extra = $extra . '</div> <!-- end of series stuff -->'; } // End of Summary posts series code } // End of what to do if series has several links

$newcontent = $content . $extra; endif; // End of series code

return $newcontent; } // ------------------------------------------------------

add_filter('the_content', 'mrk_theme_article_footer');[/php]