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 . '–' . $current_date;
}
}
if ( ! function_exists( 'copyright_years' ) ) {
function copyright_years( $earliest_id = null ) {
echo get_copyright_years( $earliest_id );
}
}
?>
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.
functions.php
footer.php
use the function <?php copyright_years(); ?>
where you want the years to appear<div class="site-footer">
<p class="copyright">© <?php copyright_years(); ?>. All Rights Reserved.</p>
</div>