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.
14 Comments
“This may be a bit cryptic…” Ahhhhhhh! You totally stretch my reading comprehension skills with this stuff. And I want to dig in and try it out so badly, but I’m just chicken now, because I haven’t messed around with my files in so long now. I need to get back in the saddle again. How many times have I said this now? Hmmm?
Kelly, no worries. It’s really powerful stuff, but much is implied here in terms of picking-up some coding knowledge about includes etc. You could do it, but it will take a little background reading. That said, if you get to the point where you want to start doing more major customization of a site, it’s a handy tool. The built-in template system for sidebars may be a better place to start. I’ll try and write a post on that feature.
Thanks, Phil. I’m going to have to start being more serious about all of this. It does interest me, and I’m curious. I just need to treat it like I did school…ARG!
Very cool, I’ve been looking for similar code but have not found it yet. I need to include certain file if it is certain post (post id).How would I implement that? I’ve tried some code options but get errors. Could you help?
Thanks.
Damir,
I believe the following code will work for you:
< ?php global $post;
if ($post->post_id == 33 )
{include(’wp-includes/includedfile.php’); } ?>
I’ve not tested it exactly, but it’s a slight modification from the one above citing section of the site.
If that doesn’t work, I’d have to sleuth it out. You may want to check out: http://www.webdesignerwall.com/tutorials/wordpress-theme-hacks/
He has some great tips that may lead you down the right path as well.
Thanks Phil, I did figured it out actually. here’s what I’m using:
ID == 24 )
{include(TEMPLATEPATH . ‘/colorado_product.php’); } ?>
But now i’ trying to use different single.php template for either category ID or I’m not sure by what else I could define it. I got this code from codex.wordpress.org :
post;
if ( in_category(‘9′) ) {
include(TEMPLATEPATH . ‘/single2.php’);
} else {
include(TEMPLATEPATH . ‘/single1.php’);
}
?>
but I’m getting an error. maybe because it is WP 2.3.2?
do you know the code to achieve this?
Thanks.
Actually that is code using query , I’m using this one:
I tried both but get error :”Out of memory (allocated 65798144) (tried to allocate 35 bytes) in /home/….”
Damir, I’m not sure about the out of memory error. Quirky indeed.
Wondering if you may want to look at custom fields to accomplish the same objective. Sometimes approaching the goal from a different direction can get you there just as quick. It depends on the end result you are looking for.
Take a look at that. I may also have some other code along these lines I can dig-up. If you have a URL and a description of what you are wanting to accomplish, that may be helpful as well.
Thanks for the reply. I’m building a Health Insurance website and have posts with medical information and resources for each individual state and would like to include a different file with links to insurance quotes pages for specific state. It would probably be a table with image and links. see I would like to display different image and different links for each state page(post) and would like for it to be at the top of the page under header and I have not used custom fields much.
what do you think.
Thanks.
Sounds to me like Custom fields may be just what you need to match your objective. You can place custom fields anywhere on a page. They are very cool. The nice thing about it is then you don’t have to upload a new include for each page which could become a real bummer. Yeah, from the sound of it custom field would be a real workable solution for you.
Do some research on how to use custom fields in WP. There is much good info on the subject. Get back w/ me if you get stuck at some point, but I think you’ll find them fairly easy to utilize. The code is pretty straight forward.
One quick tip, first you put in the code on the page with the stated named field, then you enter that name as the key and what should show on the page in the value field. The next time you call-up a custom field, the new key will now show-up in the drop-down.
Thanks so much for your help. i will look up wordpress.org for info. I’ll get back to you with results.
Thanks again.
Sorry I just looked at wordpress site and I didn’t see the code to put in the page to display custom field. Do you have that code handy?
Thanks.
I just spent an hour working on this code trying to make it work. It continued to fail and break my page, but I finally got it working. When copying and pasting you code, only to change values, I totally forgot that those single quotes (‘) copy and paste differently. All I had to do was delete and retype the single quotes and the code works fine now. You may want to put that precaution into your help file.
Talvaran,
That’s great input. As I write other tutorials, I may just link to downloadable text files with the code. That would help solve the html rendering glitch. Thanks for the feedback.