WordPress Problem - Series Posts

NOTE: Problem fixed - code unedited from the bugged version ---------- On my private 'test-bed' installation, I thought I'd update my Series post function for compatibility with TwentyEleven Wordpress Theme and standard hooks, as opposed to Thematic.

The idea is that I add a custom field to my posts called 'Series' with a value of my choosing - and then the script adds links to other posts in the same series.

It works, in that single posts and some summary posts are formatted as required - but when a summary has NON series link posts, it breaks. I don't see why, as far as I can see it should simply return the original content unaffected if the post being processed isn't a series link. Worse, it doesn't simply break that post on a summary page, but it stops processing other posts.

As I type, I have an earlier version working on this site - and it is this which was modified - however, I can see nothing that would prevent posts appearing on summary pages.

I can't see the error - I would appreciate any insights readers may have.

On my test-bed site, in functions.php I have the following:

[php]function mrk_theme_article_footer($content) { $original_content = $content; $psttitle = get_the_title(); $pstbody = get_the_excerpt(); $siteurl = get_bloginfo('wpurl'); $themeurl = get_bloginfo('stylesheet_directory'); $pageurl = get_permalink($post->ID);

$extra = '';

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 . $postnumber . ' in'; } $extra = $extra . ' a series of ' . $seriescount .' posts which begins with <a href="'; $extra = $extra . $firstlink; $extra = $extra . '">' . $firsttitle . '</a>'; $extra = $extra . 'the latest in'; } else { $extra = $extra . $postnumber . ' in'; } $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 = $extra . '. '; } else { $extra = $extra . ', the latest post in the series is <a href="'; $extra = $extra . $lastlink . '">' . $lasttitle . '</a>.'; } $extra = $extra . '</p>'; } $extra = $extra . '</div> <!-- end of series stuff -->'; } // End of Summary posts series code } // End of what to do if series has several links

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

return $content; }

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