<?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>WordPress Hacks &#187; Code Snippets</title>
	<atom:link href="http://wphacks.com/tag/code-snippets/feed/" rel="self" type="application/rss+xml" />
	<link>http://wphacks.com</link>
	<description>WordPress Themes, Plugins, Hacks, Tutorials, and more!</description>
	<lastBuildDate>Sat, 19 May 2012 22:29:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Huge Compilation of WordPress Code</title>
		<link>http://wphacks.com/huge-compilation-of-wordpress-code/</link>
		<comments>http://wphacks.com/huge-compilation-of-wordpress-code/#comments</comments>
		<pubDate>Wed, 14 Nov 2007 09:00:55 +0000</pubDate>
		<dc:creator>Kyle Eslick</dc:creator>
				<category><![CDATA[Hacks]]></category>
		<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[WordPress Code]]></category>
		<category><![CDATA[WordPress Hacks]]></category>
		<category><![CDATA[WordPress How-To]]></category>

		<guid isPermaLink="false">http://wphacks.com/huge-compilation-of-wordpress-code/</guid>
		<description><![CDATA[If you are a theme designer, or just enjoy customizing your existing WordPress theme, it can sometimes be a hassle digging around for the WordPress code you need. I&#8217;ve spent a lot of time on this site collecting various snippets of code since launch and decided that it would probably be more convenient and useful [...]]]></description>
			<content:encoded><![CDATA[<p>If you are a theme designer, or just enjoy customizing your existing WordPress theme, it can sometimes be a hassle digging around for the WordPress code you need. I&#8217;ve spent a lot of time on this site collecting various snippets of code since launch and decided that it would probably be more convenient and useful to people if I was to consolidate them all into one post for easy reference.</p>
<p>Here are the snippets of code I&#8217;ve managed to collect. If you have any WordPress code you&#8217;d like me to add, please leave a comment below!</p>
<h3><strong>Display Recent Posts</strong></h3>
<p>Here is the code you need to display the most recent 5 posts:</p>
<p><code>&lt;?php query_posts('showposts=5'); ?&gt;<br />
&lt;ul&gt;<br />
&lt;?php while (have_posts()) : the_post(); ?&gt;<br />
&lt;li&gt;&lt;a href="&lt;?php the_permalink() ?&gt;"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/li&gt;<br />
&lt;?php endwhile;?&gt;<br />
&lt;/ul&gt;</code></p>
<h3><strong>Display Recently Updated Posts/Pages</strong></h3>
<p><code>&lt;?php<br />
$today = current_time('mysql', 1);<br />
$howMany = 5; //Number of posts you want to display<br />
if ( $recentposts = $wpdb-&gt;get_results("SELECT ID, post_title FROM $wpdb-&gt;posts WHERE post_status = 'publish' AND post_modified_gmt &lt; '$today' ORDER BY post_modified_gmt DESC LIMIT $howMany")):<br />
?&gt;<br />
&lt;h2&gt;&lt;?php _e("Recent Updates"); ?&gt;&lt;/h2&gt;<br />
&lt;ul&gt;<br />
&lt;?php<br />
foreach ($recentposts as $post) {<br />
if ($post-&gt;post_title == '') $post-&gt;post_title = sprintf(__('Post #%s'), $post-&gt;ID);<br />
echo "&lt;li&gt;&lt;a href='".get_permalink($post-&gt;ID)."'&gt;";<br />
the_title();<br />
echo '&lt;/a&gt;&lt;/li&gt;';<br />
}<br />
?&gt;<br />
&lt;/ul&gt;<br />
&lt;?php endif; ?&gt;</code></p>
<h3><strong>Display Recent Comments</strong></h3>
<p><code>&lt;?php<br />
global $wpdb;<br />
$sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID,<br />
comment_post_ID, comment_author, comment_date_gmt, comment_approved,<br />
comment_type,comment_author_url,<br />
SUBSTRING(comment_content,1,30) AS com_excerpt<br />
FROM $wpdb-&gt;comments<br />
LEFT OUTER JOIN $wpdb-&gt;posts ON ($wpdb-&gt;comments.comment_post_ID =<br />
$wpdb-&gt;posts.ID)<br />
WHERE comment_approved = '1' AND comment_type = '' AND<br />
post_password = ''<br />
ORDER BY comment_date_gmt DESC<br />
LIMIT 10";<br />
$comments = $wpdb-&gt;get_results($sql);<br />
$output = $pre_HTML;<br />
$output .= "\n&lt;ul&gt;";<br />
foreach ($comments as $comment) {<br />
$output .= "\n&lt;li&gt;".strip_tags($comment-&gt;comment_author)<br />
.":" . "&lt;a href=\"" . get_permalink($comment-&gt;ID) .<br />
"#comment-" . $comment-&gt;comment_ID . "\" title=\"on " .<br />
$comment-&gt;post_title . "\"&gt;" . strip_tags($comment-&gt;com_excerpt)<br />
."&lt;/a&gt;&lt;/li&gt;";<br />
}<br />
$output .= "\n&lt;/ul&gt;";<br />
$output .= $post_HTML;<br />
echo $output;?&gt;</code></p>
<h3><strong>Display Top Comments</strong></h3>
<p><code>&lt;?php $result = $wpdb-&gt;get_results("SELECT comment_count,ID,post_title FROM $wpdb-&gt;posts ORDER BY comment_count DESC LIMIT 0 , 10");<br />
foreach ($result as $topten) {<br />
$postid = $topten-&gt;ID;<br />
$title = $topten-&gt;post_title;<br />
$commentcount = $topten-&gt;comment_count;<br />
if ($commentcount != 0) { ?&gt;<br />
&lt;li&gt;&lt;a href="&lt;?php echo get_permalink($postid); ?&gt;" title="&lt;?php echo $title ?&gt;"&gt;&lt;?php echo $title ?&gt;&lt;/a&gt;&lt;/li&gt;<br />
&lt;?php } } ?&gt;</code></p>
<h3><strong>Display Categories</strong></h3>
<p><code>&lt;h2&gt;Categories&lt;/h2&gt;<br />
&lt;ul&gt;<br />
&lt;?php wp_list_cats('sort_column=name'); ?&gt;<br />
&lt;/ul&gt;</code></p>
<h3><strong>Display Categories in Drop-Down Box</strong></h3>
<p><code>&lt;form action="&lt;?php bloginfo('url'); ?&gt;/" method="get"&gt;<br />
&lt;?php<br />
$select = wp_dropdown_categories('show_option_none=Select category&amp;show_count=1&amp;orderby=name&amp;echo=0');<br />
$select = preg_replace("#&lt;select([^&gt;]*)&gt;#", "&lt;select$1 onchange='return this.form.submit()'&gt;", $select); echo $select; ?&gt;<br />
&lt;noscript&gt;&lt;input type="submit" value="View" /&gt;&lt;/noscript&gt;<br />
&lt;/form&gt;</code></p>
<h3><strong>Display Archives</strong></h3>
<p><code>&lt;h2&gt;Archives&lt;/h2&gt;<br />
&lt;ul&gt;<br />
&lt;?php wp_get_archives('type=monthly'); ?&gt;<br />
&lt;/ul&gt;</code></p>
<h3><strong>Display Archives in a Drop-Down Box</strong></h3>
<p><code>&lt;select name=\"archive-dropdown\" onChange='document.location.href=this.options[this.selectedIndex].value;'&gt;<br />
&lt;option value=\"\"&gt;&lt;?php echo attribute_escape(__('Select Month')); ?&gt;&lt;/option&gt;<br />
&lt;?php wp_get_archives('type=monthly&amp;format=option&amp;show_post_count=1'); ?&gt; &lt;/select&gt;</code></p>
<h3><strong>Display a Pages Menu in your Sidebar</strong></h3>
<p><code>&lt;h2&gt;Pages&lt;/h2&gt;<br />
&lt;ul&gt;<br />
&lt;?php wp_list_pages('title_li='); ?&gt;<br />
&lt;/ul&gt;</code></p>
<h3><strong>Display Gravatars (WordPress 2.5+ Only)</strong></h3>
<p><code>&lt;?php if(function_exists(’get_avatar’)){ echo get_avatar($comment, ‘50?);} ?&gt;</code></p>
<h3><strong>Display Blogroll Links </strong></h3>
<p><code>&lt;ul&gt;<br />
&lt;?php get_links_list(); ?&gt;<br />
&lt;/ul&gt; </code></p>
<h3><strong>Display Admin Section</strong></h3>
<p><code>&lt;ul&gt;<br />
&lt;?php wp_register(); ?&gt;<br />
&lt;li&gt;&lt;?php wp_loginout(); ?&gt;&lt;/li&gt;<br />
&lt;li&gt;&lt;a href="http://www.wordpress.org/"&gt;WordPress&lt;/a&gt;&lt;/li&gt;<br />
&lt;?php wp_meta(); ?&gt;<br />
&lt;li&gt;&lt;a href="http://validator.w3.org/check?uri=referer"&gt;XHTML&lt;/a&gt;&lt;/li&gt;<br />
&lt;/ul&gt;</code></p>
<h3><strong>Display a Pages SubMenu in your Sidebar</strong></h3>
<p>This will display any subpages in your blog&#8217;s sidebar:<br />
<code>&lt;?php$children = wp_list_pages('title_li=&amp;child_of='.$post-&gt;ID.'&amp;echo=0');if ($children) { ?&gt;&lt;ul&gt; &lt;?php echo $children; ?&gt;<br />
&lt;/ul&gt;<br />
&lt;?php } ?&gt;</code></p>
<h3><strong>Display WordPress Tags</strong></h3>
<p><code>&lt;?php the_tags(); ?&gt;</code></p>
<h3><strong>Display WordPress Tags Cloud</strong></h3>
<p><code>&lt;?php wp_tag_cloud('smallest=8&amp;largest=36&amp;'); ?&gt;</code></p>
<h3><strong>Template Name</strong></h3>
<p>This allows you to use the WordPress page template to customize how a page is displayed:<br />
<code>&lt;?php /* Template Name: Portfolio */ ?&gt;</code></p>
<h3><strong>Dynamic Title Tags</strong></h3>
<p><code>&lt;title&gt;&lt;?phpif (is_home()) { echo bloginfo('name');<br />
} elseif (is_404()) {<br />
echo '404 Not Found';<br />
} elseif (is_category()) {<br />
echo 'Category:'; wp_title('');<br />
} elseif (is_search()) {<br />
echo 'Search Results';<br />
} elseif ( is_day() || is_month() || is_year() ) {<br />
echo 'Archives:'; wp_title('');<br />
} else {<br />
echo wp_title('');<br />
}<br />
?&gt;&lt;/title&gt;</code></p>
<h3><strong>Display PHP on a Single Page</strong></h3>
<p>Allows you to display plugins and such on a single page (replace home with the page you want it to only appear on):</p>
<p><code>&lt;?php if ( is_home() ) { include ('file.php'); } ?&gt;</code></p>
<h3><strong>Display an External RSS Feed</strong></h3>
<p><code>&lt;?php include_once(ABSPATH.WPINC.'/rss.php');<br />
wp_rss('http://wpforums.com/external.php?type=RSS2', 5); ?&gt;</code></p>
<h3><strong>Display Most Recent Twitter Entry</strong></h3>
<p><code>&lt;?php<br />
// Your twitter username.<br />
$username = "TwitterUsername";<br />
// Prefix - some text you want displayed before your latest tweet.<br />
// (HTML is OK, but be sure to escape quotes with backslashes: for example href=\"link.html\")<br />
$prefix = "";<br />
// Suffix - some text you want display after your latest tweet. (Same rules as the prefix.)<br />
$suffix = "";<br />
$feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&amp;rpp=1";<br />
function parse_feed($feed) {<br />
$stepOne = explode("&lt;content type=\"html\"&gt;", $feed);<br />
$stepTwo = explode("&lt;/content&gt;", $stepOne[1]);<br />
$tweet = $stepTwo[0];<br />
$tweet = str_replace(”&amp;lt;”, “&lt;”, $tweet);<br />
$tweet = str_replace(”&amp;gt;”, “&gt;”, $tweet);<br />
return $tweet;<br />
}<br />
$twitterFeed = file_get_contents($feed);<br />
echo stripslashes($prefix) . parse_feed($twitterFeed) . stripslashes($suffix);<br />
?&gt;</code></p>
<p>I&#8217;m expecting this list to grow over time as I come across more useful WordPress code and update the post.</p>
<img src="http://wphacks.com/?ak_action=api_record_view&id=76&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://wphacks.com/huge-compilation-of-wordpress-code/feed/</wfw:commentRss>
		<slash:comments>72</slash:comments>
		</item>
	</channel>
</rss>

