WordPress Functions

What is is_page WordPress function, and how to use it

16 October 2023

When it comes to creating dynamic and interactive websites with WordPress, having the ability to control content and functionality on specific pages is essential. This is where the “is_page” function steps into the spotlight. This article will delve into the “is_page” function, exploring what it is, how it works, and, most importantly, how you can effectively tailor your WordPress website to your specific needs.

What is the “is_page” Function in WordPress?

WordPress’s “is_page” function is a robust conditional tag that allows you to determine whether the current query corresponds to a single page on your website. It provides a straightforward way to check if the page being displayed matches a specified condition.

Function Signature:

is_page( int|string|int[]|string[] $page = '' ): bool

Uses: Under the hood, the “is_page” function uses WP_Query::is_page(), part of the WordPress query system. It leverages WordPress’s powerful query capabilities to perform the page check.

Description: If the $page parameter is specified, this function will additionally check if the query is for one of the pages specified in the parameter.

Parameters: $page (Optional): This parameter accepts various input types to specify the pages you want to check against. You can provide the Page ID, title, slug, or an array of such values. If left empty (the default), it checks if the query is for any existing page.

Return Value: The function returns a boolean value (true or false) indicating whether the current query corresponds to an existing page.

Understanding the “is_page” function is fundamental for customizing content, styling, and functionality on specific pages of your WordPress website, enabling you to create tailored and engaging user experiences.

How does the “is_page” Function Works in WordPress?

Behind the scenes of the WordPress “is_page” function lies the intricate machinery of the Query class. This fundamental function is not merely a standalone feature; it’s deeply integrated into WordPress’s query system.

In this section, we’ll take a closer look at the inner workings of this function, exploring the elegant code that makes it all possible. Understanding this process will empower you to utilize “is_page” effectively in customizing content, styles, and functionality on specific pages of your WordPress website.

Explanation of “is_page” Function:

public function is_page( $page = '' ) {
if ( ! $this->is_page ) {
return false;
}
if ( empty( $page ) ) {
return true;
}
$page_obj = $this->get_queried_object();
if ( ! $page_obj ) {
return false;
}
$page = array_map( 'strval', (array) $page );
if ( in_array( (string) $page_obj->ID, $page, true ) ) {
return true;
} elseif ( in_array( $page_obj->post_title, $page, true ) ) {
return true;
} elseif ( in_array( $page_obj->post_name, $page, true ) ) {
return true;
} else {
foreach ( $page as $pagepath ) {
if ( ! strpos( $pagepath, '/' ) ) {
continue;
}
$pagepath_obj = get_page_by_path( $pagepath );
if ( $pagepath_obj && ( $pagepath_obj->ID == $page_obj->ID ) ) {
return true;
}
}
}
return false;
}

Initial Checks:

  1. The function begins by checking whether the global variable $this->is_page is set. The function returns false if it’s not set (meaning the current query is not for a page).
  2. Next, it checks if the $page parameter passed to the function is empty. If empty, the function returns true, indicating that the current query is for any existing single page.

Obtaining the Current Page Object:
  1. If the $page parameter is not empty and the query is indeed for a page, the function uses $this->get_queried_object() to obtain the current page object. This object represents the page currently being queried.

Checking Conditions:

  1. The function then prepares the $page parameter by converting it into an array of string values, allowing multiple values to be checked against.
  2. It checks if the ID of the current page matches any of the values in the $page array or if the page’s title or slug (post_name) matches any of the values. If these conditions are met, the function returns true, indicating that the current query is for the specified page(s).

Checking Paths:

  1. The function enters a loop to check paths if none of the above conditions are met. If a value in the $page array contains a forward slash ‘/,’ it’s assumed to be a page path.
  2. The function retrieves the page object for the path using get_page_by_path(). If a valid object is found and its ID matches the ID of the current page, the function returns true.

Fallback:

  1. If none of the conditions in the loop match, the function returns false as a fallback, indicating that the current query does not correspond to any specified conditions. 

In summary, the “is_page” function uses a series of checks and comparisons to determine whether the current query in WordPress corresponds to a specified page or several pages, providing a flexible way to execute code or apply custom styles conditionally.

For more detailed information on “is_page” and related theme functions, refer to the “Conditional Tags” article in the Theme Developer Handbook.

Using the “is_page” Function Effectively in WordPress

Now, let’s explore some practical examples of how to use the “is_page” function to enhance your WordPress website’s functionality and appearance:

Example 1: Parent Page and Sub Pages Conditional Content Display
global $post;
if (is_page()) {
if ($post->post_parent) {
echo '<h1>Child page: ' . $post->post_title . '</h1>';
echo '<p>You can display content specific to child pages here.</p>';
} else {
echo '<h1>Page parent: ' . $post->post_title . '</h1>';
echo '<p>You can display content specific to parent pages here.</p>';
}
} else {
echo '<p>You can handle other content types or scenarios here.</p>';
}

In this enhanced example, the code checks if it is a parent or child page and dynamically outputs the title.

Such granular control enables you to craft a WordPress experience that feels responsive and deeply integrated, weaving the user experience into the very fabric of your site architecture.

Example 2: Unique Styling For the Existing Single Page
if (is_page('portfolio')) {
echo '<style>
.header {
background-color: #f5f5f5;
color: #333;
}
</style>';
}

In this case, the “is_page” function checks if the current page is the “Portfolio” page. If so, it applies custom CSS styles, providing a unique visual appearance. This technique is helpful for branding and distinguishing different sections of your website.

Example 3: Display Meta Data on a Specific Page

global $post;
if (is_page('about-me')) {
$about_page_subtitle = get_post_meta($post->ID, 'about_page_subtitle', true);
$about_page_featured_image = get_post_meta($post->ID, 'about_page_featured_image', true);
if ($about_page_subtitle) {
echo '<h2 class="about-subtitle">' . esc_html($about_page_subtitle) . '</h2>';
}
if ($about_page_featured_image) {
echo '<img src="' . esc_url($about_page_featured_image) . '" alt="Featured Image" />';
}
} elseif (is_page('contact')) {
$contact_page_location = get_post_meta($post->ID, 'contact_page_location', true);
if ($contact_page_location) {
echo '<p class="contact-location">Our office is located at: ' . esc_html($contact_page_location) . '</p>';
}
}

In this example, when the “About” page is displayed (is_page(‘about’)), the custom data, like a subtitle and a featured image, are fetched using get_post_meta() and displayed.

Example 4: Customized Banner for Paginated Pages

global $page;
if (is_page('blog')) {
if ($page == 1) {
echo '<div class="banner" style="background-image: url(\'first-banner.jpg\');">';
echo '<h1>Welcome to Our Blog</h1>';
echo '</div>';
} elseif ($page > 1) {
echo '<div class="banner" style="background-image: url(\'paginated-banner.jpg\');">';
echo '<h1>Welcome to Page ' . $page . ' of Our Blog</h1>';
echo '</div>';
echo '<p>You are viewing page ' . $page . ' of the blog.</p>';
}
}

In this example, the code checks if the user is on the ‘blog’ pages and determines the current page number. Based on this information, it dynamically sets the banner image and title.

  • An image specific to the first page is displayed, along with a welcoming header.
  • On any paginated page, a different image is shown.

By judiciously using the “is_page” function, you can create dynamic and tailored experiences for your website visitors, ensuring that each page delivers the content and style you desire. Whether building a blog, a corporate website, or an e-commerce platform, understanding and leveraging “is_page” can elevate your WordPress development skills to new heights.

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