jdsteinbach
1/4/2016 - 6:30 PM

Automatically Generate WP Copyright Range of Dates

Automatically Generate WP Copyright Range of Dates

<?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 );
  }
}
?>

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>