17
Jun
Quick Tip #3 - Writing your own ‘is_page’ function similar to the one in WordPress
Posted by Nouveller in Quick Tips, WordPress
Posted by Nouveller in Quick Tips, WordPress
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.
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>
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; } } ?>
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.
My name is Benjamin Reid, I live in the UK and keep the magic locked into this site.
This entry was posted on Wednesday, June 17th, 2009 at 7:43 pm and is filed under Quick Tips, WordPress & tagged with Download, Functions, HTML, WordPress, XHTML. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Liam Goodacre wrote on June 17, 2009
Cool, nice tip
used something similar to this awhile back, where I used a ’select’ to determine which page content to include, in relation to what the current page title was.
–Liam Goodacre
WordPress Tips From Twitter, Digest #1 | Weblog Tools Collection wrote on June 18, 2009
[...] Writing your own is_page function – By default WordPress allows you to check whether users are on a page using is_page() function, these tips will help you determine which exact page users are on currently and display appropriate messages to them. [...]
JD Hartley wrote on June 18, 2009
Looks great!
You could even simplify the title tag
To this:
Thanks for the code, Benjamin.
-JD
Xs wrote on December 20, 2009
function is_page($current_page, $page, $title){
if($current_page == $page)
echo $title;
}
This function could save you a bit more of time
Marc wrote on January 16, 2010
Hey Benjamin, thanks for this helpful little post. It’s exactly what I was looking for. I do have a problem though and I was wandering if you’d help me solve it?
I pull my header[dot]php with the PHP function requier into all my pages. Now all of my pages are having the same name.
I’m not good enough to find a work around myself. Could you help me with a quick fix? Many thanks Marc
Nouveller wrote on January 17, 2010
Instead of defining $page in the page title just add it above you’re header.php include, that should sort it out. If you’re still having problems, send me a message using my contact form.
Marc wrote on January 17, 2010
Thanks Benjamin, I just moved the entire title tag including you PHP snippet from above onto my page befor my require (include) header. It works perfect!
Nouveller wrote on January 18, 2010
Great! Glad you got it sorted.