<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Frumph.NET &#187; code snippet</title>
	<atom:link href="http://frumph.net/tag/code-snippet/feed/" rel="self" type="application/rss+xml" />
	<link>http://frumph.net</link>
	<description>Just another WordPress site</description>
	<lastBuildDate>Sat, 12 May 2012 15:08:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Random Post Widget</title>
		<link>http://frumph.net/2009/09/21/random-post-widget/</link>
		<comments>http://frumph.net/2009/09/21/random-post-widget/#comments</comments>
		<pubDate>Mon, 21 Sep 2009 08:08:07 +0000</pubDate>
		<dc:creator>Philip M. Hofer (Frumph)</dc:creator>
				<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[code snippet]]></category>
		<category><![CDATA[theme]]></category>
		<category><![CDATA[widget]]></category>

		<guid isPermaLink="false">http://frumph.net/?p=222</guid>
		<description><![CDATA[Navigation is a big issue with *alot* of people. People want to find your content or find a way to &#8216;get&#8217; to your content. One of ways you can do that is to actually have something that makes it so [&#8230;] <a class="more-link" href="http://frumph.net/2009/09/21/random-post-widget/">&#8595; Read the rest of this entry...</a>]]></description>
			<content:encoded><![CDATA[<p>Navigation is a big issue with *alot* of people.  People want to find your content or find a way to &#8216;get&#8217; to your content.  One of ways you can do that is to actually have something that makes it so that users can go to a post of yours that&#8217;s completely random.</p>
<p>In ComicPress, Tyler Martin created a Random Comic widget which is very useful to have people go to any number of comics in the archive.   I&#8217;ve adapted the widget to be random posts, a pick of all of them and converted it to a WordPress 2.8 + widget.</p>
<p><span id="more-222"></span></p>
<p>Let&#8217;s start off by writing the line of code that looks for a word in the URI line:</p>
<pre class="brush: php; title: ; notranslate">
if ( isset( $_GET['randompost'] ) )
	add_action( 'template_redirect', 'random_post' );
</pre>
<p>What this line does it looks for /?randompost at the end of a URI line and adds an action to the <em>template_redirect</em> function that executes the <em>function random_post</em>.  Because of the questionmark before randompost the uri line will think that it&#8217;s a uri variable container and wants a variable for it, but we dont need one, just setting it is enough.</p>
<p>Now let&#8217;s write the function random_post that gets run when it finds randompost in the uri line.</p>
<pre class="brush: php; title: ; notranslate">
//Generate a random post page - to use simply create a URL link to &amp;quot;/?randompost&amp;quot;
function random_post() {
	$randomPostQuery = new WP_Query(); $randomPostQuery-&amp;gt;query('showposts=1&amp;amp;orderby=rand');
	while ($randomPostQuery-&amp;gt;have_posts()) : $randomPostQuery-&amp;gt;the_post();
		$random_post_id = get_the_ID();
	endwhile;
	wp_redirect( get_permalink( $random_post_id ) );
	exit;
}
</pre>
<p>This function random_post creates a new query since $wp_query and $post are not being read as a global will totally avoid it.   In the query line it asks for 1 post and orders it by rand which means randomize the entire posts archive but only show one post.   Then what it does it does a while loop of all the posts it finds and sets $random_post_id to the ID of the post.  Then redirect by getting the permalink that $random_post_id points to.</p>
<p>Okay so far we&#8217;ve got this, we&#8217;ve got the ability to read <em>randompost</em> from the URI line with a $_GET and then we have it execute the function to actually redirect users to a random post on your system.</p>
<p>But how do we embed it on your site?   Well you could just make a static link that goes   <a href="/?randompost">Random Post</a>  that would work just fine, because all your doing is wanting to set the URI line.   You can do that with a text widget very easily.   &#8230; I wrote a WordPress 2.8/2.8.4+ widget that will do it for you however so you can just adjust it&#8217;s position in your sidebars how you see fit.</p>
<pre class="brush: php; title: ; notranslate">
class widget_random_post extends WP_Widget {

	function widget_random_post() {
		$widget_ops = array('classname' =&amp;gt; 'widget_comicpress_random_post', 'description' =&amp;gt; 'Displays a link to click to trigger a random blog post.' );
		$this-&amp;gt;WP_Widget('random_post', 'Random Post', $widget_ops);
	}

	function widget($args, $instance) {
		global $post;
		extract($args, EXTR_SKIP);

		echo $before_widget;
		$title = empty($instance['title']) ? '' : apply_filters('widget_title', $instance['title']);
		if ( !empty( $title ) ) { echo $before_title . $title . $after_title; }; ?&amp;gt;
			&amp;lt;h2&amp;gt;&amp;lt;a href=&amp;quot;?randompost&amp;quot;&amp;gt;&amp;lt;span class=&amp;quot;random-post-icon&amp;quot;&amp;gt;?&amp;lt;/span&amp;gt; Random Post&amp;lt;/a&amp;gt;&amp;lt;/h2&amp;gt;
		&amp;lt;?php
		echo $after_widget;
	}

	function update($new_instance, $old_instance) {
		$instance = $old_instance;
		$instance['title'] = strip_tags($new_instance['title']);
		return $instance;
	}

	function form($instance) {
		$instance = wp_parse_args( (array) $instance, array( 'title' =&amp;gt; '' ) );
		$title = strip_tags($instance['title']);
		?&amp;gt;
		&amp;lt;p&amp;gt;&amp;lt;label for=&amp;quot;&amp;lt;?php echo $this-&amp;gt;get_field_id('title'); ?&amp;gt;&amp;quot;&amp;gt;Title: &amp;lt;input class=&amp;quot;widefat&amp;quot; id=&amp;quot;&amp;lt;?php echo $this-&amp;gt;get_field_id('title'); ?&amp;gt;&amp;quot; name=&amp;quot;&amp;lt;?php echo $this-&amp;gt;get_field_name('title'); ?&amp;gt;&amp;quot; type=&amp;quot;text&amp;quot; value=&amp;quot;&amp;lt;?php echo attribute_escape($title); ?&amp;gt;&amp;quot; /&amp;gt;&amp;lt;/label&amp;gt;&amp;lt;/p&amp;gt;
		&amp;lt;?php
	}
}
register_widget('widget_random_post');

function widget_random_post_init() {
	new widget_random_post();
}

add_action('widgets_init', 'widget_random_post_init');
</pre>
<p>This is a standard 2.8 widget.   You can then go to Appearance -&gt; Widgets and add it to any of your sidebars.</p>
<p>Just like Tyler did with ComicPress&#8217;s Random Comic I set a class inside the widget so you can place an icon image inside there if you wanted to, or in this case just make the ? mark look nicer.</p>
<pre class="brush: php; title: ; notranslate">
&amp;lt;span class=&amp;quot;random-post-icon&amp;quot;&amp;gt;?&amp;lt;/span&amp;gt;
</pre>
<pre class="brush: css; title: ; notranslate">
.random-post-icon {
	padding: 0 5px;
	color: #fff;
	background: #000;
}
</pre>
<p>You of course can change the CSS to look how you want to for an icon.</p>
<p>Have fun!</p>
<p>- Phil (Frumph)</p>
 <img src="http://frumph.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=222" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://frumph.net/2009/09/21/random-post-widget/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Extended Body Classes &#8211; Revisited</title>
		<link>http://frumph.net/2010/05/11/extended-body-classes/</link>
		<comments>http://frumph.net/2010/05/11/extended-body-classes/#comments</comments>
		<pubDate>Tue, 11 May 2010 07:01:53 +0000</pubDate>
		<dc:creator>Philip M. Hofer (Frumph)</dc:creator>
				<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[code snippet]]></category>
		<category><![CDATA[theme]]></category>

		<guid isPermaLink="false">http://frumph.net/?p=220</guid>
		<description><![CDATA[WordPress 2.8+ gave us a a lot of new sets of classes for the body post and comment areas. That will allow designers to use those tags in designing their site. These tags are &#8216;dynamically generated&#8217; when certain events or [&#8230;] <a class="more-link" href="http://frumph.net/2010/05/11/extended-body-classes/">&#8595; Read the rest of this entry...</a>]]></description>
			<content:encoded><![CDATA[<p>WordPress 2.8+ gave us a a lot of new sets of classes for the body post and comment areas.  That will allow designers to use those tags in designing their site.   These tags are &#8216;dynamically generated&#8217; when certain events or pages are loaded.   To enable your theme to use those tags you just have to do a little editing to your theme.</p>
<pre class="brush: php; title: ; notranslate">
&lt;body &lt;?php if (function_exists('body_class')) { body_class(); } &gt;&gt;
</pre>
<p>On Frumph.NET and other WordPress / ComicPress sites, there&#8217;s a plethora of different customizations you can do.   Per page, per category, if the page is in the comic category and more.  In example on this site for this page if you were to go to page 2 of this post.</p>
<pre class="brush: plain; title: ; notranslate">
&lt;body class=&quot;single single-post postid-220 logged-in paged-2 single-paged-2 user-admin sitemember noncomic gecko single-category-wordpress single-author-admin am day morning tue layout-3c&quot;&gt;
</pre>
<p>All of these can be used to design your site by just placing one of those before the element you want to use.<br />
<span id="more-220"></span></p>
<p>This says, if the function exists, body_class then go ahead and run that function.   Using function_exists allows WordPress installations that do not have that function installed to basically bypass running that function if it&#8217;s not available.</p>
<p>In ComicPress versions 2.8 and higher, I have included a function that extends the abilities of the body_class function giving even more dynamically generated tags.</p>
<h4>The Extended Body Classes</h4>
<p><strong>User Classes</strong><br />
.user-userloginname<br />
.user-guest<br />
.sitemember<br />
.non-sitemember</p>
<p><strong>Browser Classes</strong><br />
.lynx<br />
.gecko<br />
.opera<br />
.ns4<br />
.safari<br />
.chrome<br />
.ie<br />
.unknown<br />
.iphone</p>
<p><strong>Body Post Classes (Index/Home Page)</strong><br />
.sticky-post</p>
<p><strong>Body Post Classes (Single Page)</strong><br />
.single-category-thecategorynamethepostisin<br />
.single-author-theauthorofthepost</p>
<p><strong>Time of Day Classes</strong><br />
.am<br />
.pm<br />
.midnight  &#8211; 11:30pm to 12:30am<br />
.night &#8211; 12:30am to 6am<br />
.morning &#8211; 6am to 11:30am<br />
.noon &#8211; 11:30am to 12:30pm<br />
.day &#8211; 12:30pm to 6pm<br />
.evening &#8211; 6pm to 11:30pm</p>
<p><strong>Attachment Classes</strong><br />
.attachment<br />
.attachment-ID#  &#8211; The ID# of the attachment in the post<br />
.attachment-type &#8211; The type of attachment it is</p>
<p><strong>Layout Classes</strong><br />
.layout-standard<br />
.layout-3c<br />
.layout-gn<br />
.layout-v<br />
.layout-v3c</p>
<p>Next Page, The Extended Body Class Function</p>
 <img src="http://frumph.net/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=220" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://frumph.net/2010/05/11/extended-body-classes/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

