Using WordPress ‘is_year()’ PHP function

The is_year() WordPress PHP function determines whether the query is for an existing year archive.

Usage

if (is_year()) {
    // Your custom code here
}

Parameters

  • None

More information

See WordPress Developer Resources: is_year()

Examples

Display a message on year archive pages

If the current page is a year archive, display a message.

if (is_year()) {
    echo "You are browsing the year archive!";
}

Show a custom year archive title

Customize the year archive title.

if (is_year()) {
    echo "Year Archive: " . get_the_date('Y');
}

Add a class to the body tag on year archive pages

Add a specific class to the <body> tag for year archive pages.

function my_body_classes($classes) {
    if (is_year()) {
        $classes[] = 'year-archive';
    }
    return $classes;
}
add_filter('body_class', 'my_body_classes');

Load a different template for year archives

Load a custom template for year archive pages.

function my_year_archive_template($template) {
    if (is_year()) {
        $template = locate_template(['year-archive.php']);
    }
    return $template;
}
add_filter('template_include', 'my_year_archive_template');

Modify the query on year archive pages

Change the number of posts displayed on year archive pages.

function my_year_archive_query($query) {
    if ($query->is_year() && $query->is_main_query()) {
        $query->set('posts_per_page', 12);
    }
}
add_action('pre_get_posts', 'my_year_archive_query');