Powerful WordPress Include Hacks

Customizing WordPress to function as a full-blown Content Managed System is really not that difficult. If one takes a little time and does some research you can extend it in some pretty nifty directions.

One simple and powerful function is to use an if statement to test a condition for a specific page and then include a file if it is true. This enables you to post a graphic or have a block of text show-up in locations on the page that are not defined explicitly within a sidebar for instance.

First example, the simple conditional include file

If I wanted a button, block of text or any other feature to show-up just in the sidebar, header or any other part of the page, I could slip-in this short piece of code:

<?php if (is_page(‘portfolio’))
{include(‘wp-includes/includedfile.php’); } ?>

The first line “is_page” is followed by ” ‘portfolio’ ” which delineates the page on which it should show-up. If I wanted it to show on the home page, it should simply be “is_home()”. The path “wp-includes” is where I decided to stick the included file and “includedfile.php” would be renamed to a relevant file name for its purpose. The file would be a standard php file format, which can be as simple as a text file. This example assumes that you are using a permalink structure such as simple /%postname%/ as apposed to a number ID.

Second example, include file for a section of your site

 If we decide to get a little more estute and have a multi-tiered site with sub-pages, we can have the sub or child pages of the Portfolio page include a file. This could be a breadcrumb navigation or an image for instance.

<?php global $post;
if ($post->post_parent == 33 )
 {include(‘wp-includes/includedfile.php’); } ?>

In this example the parent page is necessarily delineated by the page ID even if you are using a different permalink structure. The “==” asks a question in an integer format meaning it’s asking a mathematical question; is the parent’s ID 33? If the answer is yes, then include the file, if not then ignore. (The page parent is assigned to the page when writing or editing the page in the right-hand column).

These are just a few quick examples of how to extend a template a bit with some very basic php code. For those very new to coding, this may be a bit cryptic, but if you need some help or have any questions fire away as I’m always glad to help.

About webadmin