WordPress Functions

WordPress: is_archive Function

3 November 2023

In WordPress, every blog, portfolio, and e-commerce website has a special content structure: Categories, tags, authors, and dates neatly organize post collections. Selectively displaying this data presents a unique challenge. Conditionally customizing the look and feel of various pages, such as category, tag, or author archives, can hugely impact your site’s navigation and user experience. This is where the WordPress conditional function ‘is_archive()’ makes our lives easier. Automatically differentiating archive pages from the rest, it becomes the lynchpin in creating conditional custom designs for an array of archive listings. Not only does ‘is_archive()’ bring consistency and accuracy to custom archive designs, but it also adds a layer of fine-tuned control to our WordPress toolbox.

Suppose you run a bustling online magazine with multiple authors, categories, and topics as diverse as the colors in a rainbow. With this variety, your readers would appreciate if each author’s article archive had a personalized layout that reflected their unique writing style and spirit. Meanwhile, category pages require a more universal look. Using the ‘is_archive()’ function, you can fluidly add diverse visual punctuation to different archive types enhancing your site’s aesthetics and functionality, providing your readers with a tailored browsing experience.

Syntax

is_archive(): bool

Parameters

This function does not take any parameters.

Return Value

This function returns a boolean value, true if the current query is for an existing archive page, and false otherwise.

How it works

<?php
/**
* Determines whether the query is for an existing archive page.
*
* Archive pages include category, tag, author, date, custom post type,
* and custom taxonomy based archives.
*
* For more information on this and similar theme functions, check out
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
* Conditional Tags} article in the Theme Developer Handbook.
*
* @since 1.5.0
*
* @see is_category()
* @see is_tag()
* @see is_author()
* @see is_date()
* @see is_post_type_archive()
* @see is_tax()
* @global WP_Query $wp_query WordPress Query object.
*
* @return bool Whether the query is for an existing archive page.
*/
function is_archive() {
global $wp_query;
if ( ! isset( $wp_query ) ) {
_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
return false;
}
return $wp_query->is_archive();
}

The is_archvive() function determines whether the current query is for an existing archive page in WordPress. Archive pages include category, tag, author, date, custom post type archives, and custom taxonomy archives.

To use this function successfully, you need to have a basic understanding of conditional tags in WordPress. Conditional tags are used to check various conditions or rules about the current page being viewed. They help determine what content should be shown or hidden based on specific criteria.

Within the is_archive() implementation, the global variable $wp_query is accessed to check if it has been defined. If $wp_query is not set, this indicates that the query has not been run yet. In that case, a warning message is displayed using _doing_it_wrong(), and false is returned.

If $wp_query is set, the function calls the is_archive() method of the global $wp_query, which checks if the query is for an archive page. The result of this method call is then returned as the function’s output.

This function serves as a convenient way to determine if the current query corresponds to an archive page when developing themes or plugins in WordPress.

Usage

The is_archive() function is used to determine whether the current page being queried is an archive page. Archive pages can include category, tag, author, date, custom post type, and custom taxonomy based archives.

Example 1: Inside a Plugin

Let’s say you are developing a plugin that adds custom functionality to category archive pages. You want to display a special message on category archive pages only.

<?php
add_action('pre_get_posts', 'my_plugin_custom_function');
function my_plugin_custom_function($query)
{
if (is_archive() && is_category())
{
echo "Welcome to the Category Archive Page!";
}
}

In this example:

  • The pre_get_posts action hook is used to modify the query before the posts are fetched.
  • The my_plugin_custom_function function is hooked into the pre_get_posts action.
  • Inside the function, we use the is_archive() function to check if the current page being queried is an archive page and then use the is_category() function to further narrow it down to category archive pages.
  • If both conditions are met, we display a message saying “Welcome to the Category Archive Page!”.

Example 2: Inside a Theme

Let’s say you have a custom post type called “Portfolio” and you want to display different content on portfolio archive pages compared to regular post archive pages.

<?php
if (is_archive() && !is_post_type_archive())
{
// Regular Post Archive Page
get_template_part('content', 'archive');
}
elseif (is_post_type_archive('portfolio'))
{
// Portfolio Archive Page
get_template_part('content', 'portfolio-archive');
}

In this example:

  • We use conditional statements within our theme template file to determine which content should be displayed based on the type of archive page.
  • We use the is_archive() function to check if it is an archive page.
  • We use the is_post_type_archive() function with a negative condition (!) to identify regular post archive pages.
  • If it is a regular post archive page, we include the 'content-archive' template part, which is responsible for displaying posts in a specific format.
  • If it is a portfolio archive page (specified by the custom post type “portfolio”), we include the 'content-portfolio-archive' template part, which can have different styling or additional content specific to portfolio items.

By using conditional checks with is_archive() and related functions, you can customize the content and layout of different types of archive pages within your theme.

Leave a Reply

Your email address will not be published. Required fields are marked *

Array

Some of Our Clients

Join clients who enjoy 96% satisfaction

Schedule a Free Strategy Call with a WordPress Expert