<?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>WP First Aid &#187; get_posts</title>
	<atom:link href="http://wpfirstaid.com/tag/get_posts/feed/" rel="self" type="application/rss+xml" />
	<link>http://wpfirstaid.com</link>
	<description>It&#039;s WordPress ... anything is possible!</description>
	<lastBuildDate>Tue, 17 Jan 2012 16:47:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Dynamic Copyright</title>
		<link>http://wpfirstaid.com/2010/02/dynamic-copyright/</link>
		<comments>http://wpfirstaid.com/2010/02/dynamic-copyright/#comments</comments>
		<pubDate>Tue, 16 Feb 2010 23:36:43 +0000</pubDate>
		<dc:creator>The Doctor</dc:creator>
				<category><![CDATA[Functions]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[bns_dynamic_copyright]]></category>
		<category><![CDATA[dynamic_copyright]]></category>
		<category><![CDATA[get_posts]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://wpfirstaid.com/?p=363</guid>
		<description><![CDATA[Add a dynamic copyright function to your WordPress theme. Just use the sample code and instructions in this post.]]></description>
			<content:encoded><![CDATA[<p>At the bottom of most blogs you will find the commonplace copyright notice that may look something like this:</p>
<blockquote><p>Copyright &copy; 2010 <strong>WP First Aid</strong> All rights reserved.</p></blockquote>
<p>Which begs the question, what about those posts made before the year 2010? This is where a dynamic copyright function would come in very handy. It would be more correct to have this display in the footer:</p>
<blockquote><p>Copyright &copy; 2009-2010 <strong>WP First Aid</strong> All rights reserved.</p></blockquote>
<p>Most <a href="http://wordpress.org/">WordPress</a> <a href="http://wordpress.org/extend/themes/">themes</a> will include, at a minimum, code that will generate a <em>current year</em> copyright statement, perhaps something along these lines:</p>
<pre class="brush: xml; title: ; notranslate">&lt;?php _e( 'Copyright &amp;copy; ' ); ?&gt;
&lt;?php echo date( 'Y' ); ?&gt;
&lt;strong&gt; &lt;?php bloginfo( 'name' ); ?&gt; &lt;/strong&gt;
&lt;?php _e( 'All rights reserved.' ); ?&gt;</pre>
<p>To make this code into a function and add some dynamics lets start with a simple PHP construct:</p>
<pre class="brush: php; title: ; notranslate">function dynamic_copyright() {
  // code goes here
}</pre>
<p>To get the first year a post was written on the blog we need to find the first post. This can be done with this code snippet:</p>
<pre class="brush: php; first-line: 2; title: ; notranslate">
  $all_posts = get_posts( 'order=ASC' );
  $first_post = $all_posts[0];
  $first_date = $first_post-&gt;post_date_gmt;
</pre>
<p>We started by getting all of the posts with the <a href="http://codex.wordpress.org/Function_Reference/get_posts">get_posts()</a> function. We assigned the first post, which is found at index 0 (zero) to its own variable. Then we assign that post&#8217;s date (in GMT) to its own variable.</p>
<p>We now have isolated the date of the first post on the blog and can perform further operations on it. The basic dynamic part of the function is complete. If the first post is removed and the &#8220;new&#8221; first post has a different date, then the above snippet will simply use the new post&#8217;s date.</p>
<p>The balance of the function is only a slight modification of the original &#8220;minimum&#8221; sample code from above:</p>
<pre class="brush: php; first-line: 5; highlight: [6]; title: ; notranslate">
  _e( 'Copyright &amp;copy; ' );
  echo substr( $first_date, 0 ,4 ) . &quot;-&quot; . date( 'Y' );
  echo ' &lt;strong&gt;' . get_bloginfo( 'name' ) . '&lt;/strong&gt; ';
  _e( 'All rights reserved.' );
</pre>
<p>The key is highlighted at line 6. We <strong>echo</strong>, or display, the first four characters of the <strong>$first_date</strong> string which is the year; add a hyphen; and, then call a standard PHP function to add the current year as a four digit representation.</p>
<p>Here is the entire <strong>dynamic_copyright()</strong> function, including comments:</p>
<pre class="brush: php; title: ; notranslate">function bns_dynamic_copyright() {
  /* Get all posts */
  $all_posts = get_posts( 'post_status=publish&amp;order=ASC' );
  /* Get first post */
  $first_post = $all_posts[0];
  /* Get date of first post */
  $first_date = $first_post-&gt;post_date_gmt;

  /* Display common footer copyright notice */
  _e( 'Copyright &amp;copy; ' );
  /* Display first post year and current year */
  if ( substr( $first_date, 0, 4 ) == date( 'Y' ) ) {
  /* Only display current year if no posts in previous years */
    echo date( 'Y' );
  } else {
    echo substr( $first_date, 0, 4 ) . &quot;-&quot; . date( 'Y' );
  }
  /* Display blog name from 'General Settings' page */
  echo ' &lt;strong&gt;' . get_bloginfo( 'name' ) . '&lt;/strong&gt; ';
  _e( 'All rights reserved.' );
}
</pre>
<p>Of course, this function is compliments of <a href="http://buynowshop.com/">BuyNowShop.com</a> and has their customary <strong>bns</strong> prefix as it will be appearing in all of their themes.</p>
<p>Just like I did, you can copy and paste the entire sample <strong>bns_dynamic_copyright()</strong> code above into your theme&#8217;s <em>functions.php</em> file; then, replace the code in your theme&#8217;s <em>footer.php</em> file that generates your old <em>static</em> copyright notice with this line of code:</p>
<pre class="brush: xml; title: ; notranslate">&lt;?php bns_dynamic_copyright(); ?&gt;</pre>
<p>Your theme will now display a dynamic copyright notice as you continue your writings for many years to come. Congratulations!</p>
]]></content:encoded>
			<wfw:commentRss>http://wpfirstaid.com/2010/02/dynamic-copyright/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

