My Distractions, the almighty Elsewhere Archive : Update

DistractionIn the comments on my previous post about the little sidenotes I’m serving here, which I like to call ‘Elsewhere’, my pal Manuel threw in a term I hadn’t seen before (actually I had, but I simply forgot about it): ‘Asides’. Soon my search on Google started and all my questions got answered as you might have noticed from looking at the bram.us feed: yes, the Elsewhere entries are now included in the feed thanks to a nifty plugin named MiniPosts


About MiniPosts

Let me first say that MiniPosts is an awesome plugin! It provides you some nice options from within the admin panel, such as “Filter mini posts from the Loop” (which I’m using of course), “Filter mini posts from subscription feeds” (which I’m not using, cos’ my intent was to include these in the main feed), along with some (real handy) formatting options.

The MiniPosts Options Page
The MiniPosts Options Page

Above that it give me the thing I wanted: merge from the elsewhere entries with the main feed and the ability to let the readers comment on an Elsewhere entry

Diving in the code

Yet, I wasn’t 100% happy with the options provided. One of the things I disliked was the fact that when calling the get_mini_posts() function from within the (modded) theme I’m using always got wrapped into a div. As my sidebar (which is placed at the bottom in 3 cols) is using lists (gotta luv’ them lists), the generated output did not validate (viz. <ul><div>…</div></ul> is NOT valid). A quick dive in the code of the plugin fixed that, just look somewhere around line #395 (actually I modded the function so that it takes 2 extra params: $preWrap and $postWrap to prepend an append around the stuff it outputs). Then started fiddling with the formatting on how the Elsewhere entries should appear and finally came up with this, as it suits my needs:

<li><a href="%permalink%" title="%title" style="font-variant: small-caps; color: #996633;">%title%</a><br />%post%<br /><span class="date">[ %date% - %commentcount% comment(s) ]</span></li>

The fQuick strikes back!

Another thing I wanted was to have one page listing all the Elsewhere entries, just as the fQuick plugin – which I used to use – provides. Based upon the code from the fQuick plugin and the MiniPosts get_mini_posts() function I knocked up a real quick and dirty function which does the trick and hooked it as a fileter to the WordPress ‘the_content’ event (or how do they call that?). To have this functionality yourself, open up the miniposts2.php file and add this just before the ?> (make sure that ?> is the last line of the file.

Please note that this function does not use any of the settings provided by the plugin and really was built for me. This given however, should allow you to easily adopt it to suit your own needs.

// BEGIN ADDED BY BRAMUS!
function bramus_fullArchive($content) {

	// don't pull data from the database when it's not needed ;)
	if (eregi("\<!--minipostsarchivebybramus--\>", $content)) {

		// db
		global $wpdb;

		// format we'll be using
		$format		= '<li style="margin-bottom: 15px;"><a href="%permalink%" title="%title" style="font-variant: small-caps; color: #996633; color: #333;">%title%</a> (%commentcount%)<br />%post%</li>';

		// get miniposts
		$now 		= current_time('mysql'); // guess this must be WP based, as normally NOW() can be passed in the query ...
		$sql 		= "SELECT ID, post_date, UNIX_TIMESTAMP(post_date) AS post_date_unix, post_content, post_excerpt, post_title, COUNT($wpdb->comments.comment_ID) AS numComments FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) LEFT JOIN $wpdb->comments ON ($wpdb->posts.ID = $wpdb->comments.comment_post_ID) WHERE post_date < '$now' AND post_status = 'publish' AND $wpdb->postmeta.meta_key = '_mini_post' AND $wpdb->postmeta.meta_value = '1' GROUP BY $wpdb->posts.ID ORDER BY post_date DESC LIMIT 0,999"; // AND $wpdb->comments.comment_approved = '1'
		$miniposts	= $wpdb->get_results($sql);

		// query caught some fish?
		if (sizeof($miniposts) != 0) {

			// needed for comparing date of current minipost to date of previous minipost
			$prevDate		= "dummy";
			$curDate		= "dummy";

			// loop all fish
			foreach($miniposts as $minipost) {

				// set curDate to date of current item
				$curDate 	= date("F jS Y", $minipost->post_date_unix);

				// check if a new date title should be started ...
				if ($prevDate != $curDate) {
					$out		.= (($prevDate != "dummy")?"</ul>\n\n":"")."<h3>".$curDate."</h3><ul>";
					$prevDate	= date("F jS Y", $minipost->post_date_unix);
				}

				// define the replacement params
                $permaLink		= get_permalink($minipost->ID);
                $commenturl		= "$permaLink#comments";
                $title			= strip_tags($minipost->post_title);
				$commentcount	= "<a class=\"minipost_commentlink\" href=\"$commenturl\" title=\"Comments for '$title'\">".$minipost->numComments."</a>";
				$text 			= wptexturize($minipost->post_content);		// ditched out posts having more

				// now replace it all
				$toAdd			= $format;
				$toAdd 			= str_replace('%permalink%', 	$permaLink, 	$toAdd);
				$toAdd 			= str_replace('%title%', 		$title, 		$toAdd);
				$toAdd 			= str_replace('%date%', 		$curDate, 		$toAdd);
				$toAdd 			= str_replace('%commentcount%',	$commentcount, 	$toAdd);
				$toAdd 			= str_replace('%post%', 		$text, 			$toAdd);

				// now add the item
				$out		 .= $toAdd;

			}

			$out			.= "</ul>";

		} else {
			$out = "<p>Nothing in the archive :(</p>";
		}

		// return the replaced content
		//return preg_replace("|<!--minipostsarchivebybramus-->|", $out, $content);
		return $out;

	// trigger not found, just return content
	} else {
		return $content;
	}
}

add_filter('the_content', 'bramus_fullArchive');
// END ADDED BY BRAMUS!

Now all you need to do is create a page and place the following code on it, so that it will get replaced by the full archive of your Miniposts/Asides/Distractions/whatever you like to name it:

<!--minipostsarchivebybramus-->

The result can be seen on the Elsewhere Reloaded Archive page here over at Bram.us

Last but not Least

Now that the Elsewhere entries have made it into the main feed, I wanted them to distinguish themselves from the regular posts. To do this, we have to hook a filter on the_title_rss call.

Again, open up the miniposts2.php and add this at the very bottom (again before the ?>)

// BEGIN ADDED BY BRAMUS!
function bramus_reworkRSSTitle($content) {
  if ( is_mini_post() ) {
	return "[Elsewhere] $content";
  } else {
	return "[Bram.us] $content";
  }
}
add_filter('the_title_rss', 'bramus_reworkRSSTitle');
// END ADDED BY BRAMUS!

Having made this change the feed now automatically prefixes [Elsewhere] on the title if it’s a minipost and prefixes [Bram.us] if it’s no minipost. Be sure to change Bram.us if you’ll be using it.

And now?

Nothing! I think I have given this little nice plugin some extra options. I’ve also notified it’s creator of the changes I made … hopefully he’ll be adding them in a future release.

Ciao!

B!

Published by Bramus!

Bramus is a frontend web developer from Belgium, working as a Chrome Developer Relations Engineer at Google. From the moment he discovered view-source at the age of 14 (way back in 1997), he fell in love with the web and has been tinkering with it ever since (more …)

Unless noted otherwise, the contents of this post are licensed under the Creative Commons Attribution 4.0 License and code samples are licensed under the MIT License

Join the Conversation

3 Comments

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.