After building several WordPress sites lately, I’ve been getting used to writing it’s common, but useful functions to do various different things. I’ve now come to write new sites that don’t use WordPress and I miss those functions! So I’ve created my own version of the is_page function among others. View the is_page function in the WordPress Codex.

The is_page function is great for non WordPress sites that use lots of includes, such as menus. Say if you have two different includes that are included into different types of pages, you can put them in one include and use this conditional function to detect the page.

Defining the page

First I set the page name in a value called $page and echo it into the title of the document. It’s not necessary to echo it but it makes sense to.

index.php

<title><?php $page = 'home'; echo $page; ?></title>

Writing the ‘is_page’ function

Now in my index.php file I’ll include a functions.php file at the top of the page before the doctype.

<?php
 
	require_once('inc/functions.php');
 
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 
	<title><?php $page = 'home'; echo $page;?></title>
 
</head>

Next place the is_page function into inc/functions.php. First we define the function name with a value called $current_page that we can set when writing the function. Then set the $page value to global so that it can be read from outside the function. All that’s left is a simple if statement that checks whether our page name entered into the function’s brackets matches our title we set at the top of our index.php page in $title. If $page and $current_page match, it will return true if not, it will return false.

inc/functions.php

<?php 
 
function is_page($current_page)
{
	global $page;
 
	if($page == $current_page)
	{
		return true;
 
	} else {
 
		return false;
 
	}
 
}
 
?>

Calling the ‘is_page’ function on our page

Back on the index.php add call the function and enter the matching page name.

<?php 
 
	if(is_page('home')) {
 
		echo 'Home page';
 
	}
 
?>

Now a live demo wouldn’t be much help unless you could see the un-generated code, so download the files and give it a blast. Let me know you thoughts/improvements or if you use it, below.

Author: Nouveller

My name is Benjamin Reid, I live in the UK and keep the magic locked into this site.

RSS Icon

Subscribe to the RSS feed some design goodness, get in on the upcoming software give-aways!

Digg Icon Share Quick Tip #3 - Writing your own ‘is_page’ function similar to the one in WordPress to Twitter Share Quick Tip #3 - Writing your own ‘is_page’ function similar to the one in WordPress to Delicious Share Quick Tip #3 - Writing your own ‘is_page’ function similar to the one in WordPress to Facebook

Related post(s)

Comments left by other wizards

8 Responses | Make a comment

Leave your words of wisdom

Leave a Reply

This is a Gravatar

Your name