Using WordPress ‘is_archive()’ PHP function

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

Usage

if (is_archive()) {
    // Your code here
}

Parameters

  • None

More Information

See WordPress Developer Resources: is_archive()

  • Related functions: is_category(), is_tag(), is_author(), is_date(), is_post_type_archive(), is_tax()

Examples

Display a message on archive pages

This example shows how to display a custom message only on archive pages:

if (is_archive()) {
    echo "Welcome to the archive page!";
}

Apply a different layout on archive pages

This example changes the layout for archive pages by adding an “archive” class to the body:

function my_theme_body_classes($classes) {
    if (is_archive()) {
        $classes[] = 'archive';
    }
    return $classes;
}
add_filter('body_class', 'my_theme_body_classes');

Hide sidebar on archive pages

This example hides the sidebar on archive pages:

if (!is_archive()) {
    get_sidebar();
}

Load a different header on archive pages

This example loads a custom header file named “header-archive.php” for archive pages:

if (is_archive()) {
    get_header('archive');
} else {
    get_header();
}

Add a custom title prefix for archive pages

This example adds a custom title prefix for archive pages:

function my_theme_custom_title($title) {
    if (is_archive()) {
        $title = 'Archives: ' . $title;
    }
    return $title;
}
add_filter('wp_title', 'my_theme_custom_title');