Including Files


Some of the modifications in this chapter involve including large chunks of code in a theme file such as the sidebar or footer. Another approach is to create a separate file containing the additional code. You can then reference the additional file from the source file with a single line of PHP code. Here's how.

To include a file in the theme

1.

Use your text editor to create a separate file containing the code you wish to include, for example, the AdSense code discussed earlier in this chapter.

2.

Save the new file in your theme's folder with a unique name, followed by the .php extension. For example, you might call a file containing AdSense code adsense.php (Figure 23).

Figure 23. You can save code in a separate file.


3.

Insert the following line of code where you would have inserted the code that's in the new file:

<?php include (TEMPLATEPATH . '/ filename.php'); ?>


Be sure to use the correct filename (Figure 24).

Figure 24. Then use an include statement to reference the other file. As you can see here, the search form in the sidebar is also referenced using an include statement.


4.

Save the changes.

5.

View your blog to ensure that it's working as expected.

Tip

  • The TEMPLATEPATH variable indicates the directory in which the active theme's templates are stored.





WordPress 2. Visual QuickStart Guide
WordPress 2
ISBN: 0321450191
EAN: 2147483647
Year: 2004
Pages: 142

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net