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.
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
echo it into the title of the document. It's not necessary to
echo it but it makes sense to.
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.
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
$current_page match, it will
return true if not, it will
Calling the 'is_page' function on our page
Back on the
index.php add call the function and enter the matching page name.
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.