Skip to content

Instantly share code, notes, and snippets.

@jdsteinbach
Last active January 29, 2016 17:38
Show Gist options
  • Save jdsteinbach/e2646846ede81458f0c4 to your computer and use it in GitHub Desktop.
Save jdsteinbach/e2646846ede81458f0c4 to your computer and use it in GitHub Desktop.
Automatically Generate WP Copyright Range of Dates

Automatically generate the range of years for copyrighted content in WP

This function will find the earliest post or page on your site, compare its year to the current year, and output the correct range of years for your footer copyright statement.

Usage

  • Copy the PHP code into your theme's functions.php
  • In your theme's footer.php use the function <?php copyright_years(); ?> where you want the years to appear

Example

<div class="site-footer">
  <p class="copyright">&copy; <?php copyright_years(); ?>. All Rights Reserved.</p>
</div>
<?php
if ( ! function_exists( 'get_copyright_years' ) ) {
function get_copyright_years( $earliest_id = null ) {
$earliest_args = array(
'post_type' => array( 'any' ),
'numberposts' => 1,
'orderby' => 'date',
'order' => 'ASC'
);
$get_post = $earliest_id
? get_post( $earliest_id )
: null;
if ( ! $get_post ) {
$get_post = array_shift( get_posts( $earliest_args ) );
}
$earliest_date = date( 'Y', strtotime( $get_post->post_date ) );
$current_date = date( 'Y' );
return $earliest_date == $current_date
? $current_date
: $earliest_date . '&ndash;' . $current_date;
}
}
if ( ! function_exists( 'copyright_years' ) ) {
function copyright_years( $earliest_id = null ) {
echo get_copyright_years( $earliest_id );
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment