Using PHP includes with WordPress
How many times have you been messing with your WordPress install and wished that you could add just a touch more flexibility to the whole thing? I mean, WordPress is fairly robust and has a ton of features, but inevitably you’ll want something that it doesn’t offer right out of the box. One of the best examples of this is the WordPress sidebar. The sidebar has been a source of frustration for WordPress users because of the lack of customization available – meaning that once you call <?php get_sidebar(); ?> you pretty much have to stick with whatever it spits out. Well no more! I’m about to show you how to call in a different sidebar (or any other file for that matter) using simple PHP includes.
PHP is the nectar of the gods
PHP is a wonderful thing. There is definitely a lot to learn, but one of the easiest concepts to pick up is how to include files. Whether you want to speed up your production or simply keep your site agile, includes are a must for every developer. What’s better is WordPress is built completely using PHP framework, meaning that you can use other functions, variables and includes anywhere, on any page. Brilliant!
Getting organized
If your WordPress install is based on a theme (the Default Theme for instance), you can create a new directory inside of it called ‘includes’. This will serve as a common place to store all of the include files that we will be pulling into our layout. Now let’s create a new file based on sidebar.php. Using your favorite text editor (mine’s Textmate), copy the contents of sidebar.php into a new file and call it alt_sidebar.php. Your new file should look something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | <div id="sidebar">
<ul>
<li>
<?php include (TEMPLATEPATH . '/searchform.php'); ?>
</li>
<!-- Author information is disabled per default. Uncomment and fill in your details if you want to use it.
<li><h2>Author</h2>
<p>A little something about you, the author. Nothing lengthy, just an overview.</p>
-->
<li>
<?php /* If this is a 404 page */ if (is_404()) { ?>
<?php /* If this is a category archive */ } elseif (is_category()) { ?>
<p>You are currently browsing the archives for the <?php single_cat_title(''); ?> category.</p>
<?php /* If this is a yearly archive */ } elseif (is_day()) { ?>
<p>You are currently browsing the <a href="<?php bloginfo('home'); ?>/"><?php echo bloginfo('name'); ?></a> weblog archives
for the day <?php the_time('l, F jS, Y'); ?>.</p>
<?php /* If this is a monthly archive */ } elseif (is_month()) { ?>
<p>You are currently browsing the <a href="<?php bloginfo('home'); ?>/"><?php echo bloginfo('name'); ?></a> weblog archives
for <?php the_time('F, Y'); ?>.</p>
<?php /* If this is a yearly archive */ } elseif (is_year()) { ?>
<p>You are currently browsing the <a href="<?php bloginfo('home'); ?>/"><?php echo bloginfo('name'); ?></a> weblog archives
for the year <?php the_time('Y'); ?>.</p>
<?php /* If this is a monthly archive */ } elseif (is_search()) { ?>
<p>You have searched the <a href="<?php echo bloginfo('home'); ?>/"><?php echo bloginfo('name'); ?></a> weblog archives
for <strong>'<?php echo wp_specialchars($s); ?>'</strong>. If you are unable to find anything in these search results, you can try one of these links.</p>
<?php /* If this is a monthly archive */ } elseif (isset($_GET['paged']) && !empty($_GET['paged'])) { ?>
<p>You are currently browsing the <a href="<?php echo bloginfo('home'); ?>/"><?php echo bloginfo('name'); ?></a> weblog archives.</p>
<?php } ?>
</li>
<?php wp_list_pages('title_li=<h2>Pages' ); ?>
<li><h2>Archives</h2>
<ul>
<?php wp_get_archives('type=monthly'); ?>
</ul>
</li>
<li><h2>Categories</h2>
<ul>
<?php wp_list_cats('sort_column=name&optioncount=1&hierarchical=0'); ?>
</ul>
</li>
<?php /* If this is the frontpage */ if ( is_home() || is_page() ) { ?>
<?php get_links_list(); ?>
<li><h2>Meta</h2>
<ul>
<?php wp_register(); ?>
<li><?php wp_loginout(); ?></li>
<li><a href="http://validator.w3.org/check/referer" title="This page validates as XHTML 1.0 Transitional">Valid <abbr title="eXtensible HyperText Markup Language">XHTML</abbr></a></li>
<li><a href="http://gmpg.org/xfn/"><abbr title="XHTML Friends Network">XFN</abbr></a></li>
<li><a href="http://wordpress.org/" title="Powered by WordPress, state-of-the-art semantic personal publishing platform.">WordPress</a></li>
<?php wp_meta(); ?>
</ul>
</li>
<?php } ?>
</ul>
</div> |
Now that you have a duplicate copy that you will use in place of sidebar.php, you can take out whatever you don’t want and add what you do. So for instance, let’s take out the Categories list and in it’s place add a link to our site’s RSS feed. Look at the example below to see how I went about it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | <div id="sidebar">
<ul>
<li>
<?php include (TEMPLATEPATH . '/searchform.php'); ?>
</li>
<!-- Author information is disabled per default. Uncomment and fill in your details if you want to use it.
<li><h2>Author</h2>
<p>A little something about you, the author. Nothing lengthy, just an overview.
-->
<li>
<?php /* If this is a 404 page */ if (is_404()) { ?>
<?php /* If this is a category archive */ } elseif (is_category()) { ?>
<p>You are currently browsing the archives for the <?php single_cat_title(''); ?> category.</p>
<?php /* If this is a yearly archive */ } elseif (is_day()) { ?>
<p>You are currently browsing the <a href="<?php bloginfo('home'); ?>/"><?php echo bloginfo('name'); ?></a> weblog archives
for the day <?php the_time('l, F jS, Y'); ?>.</p>
<?php /* If this is a monthly archive */ } elseif (is_month()) { ?>
<p>You are currently browsing the <a href="<?php bloginfo('home'); ?>/"><?php echo bloginfo('name'); ?></a> weblog archives
for <?php the_time('F, Y'); ?>.</p>
<?php /* If this is a yearly archive */ } elseif (is_year()) { ?>
<p>You are currently browsing the <a href="<?php bloginfo('home'); ?>/"><?php echo bloginfo('name'); ?></a> weblog archives
for the year <?php the_time('Y'); ?>.</p>>
<?php /* If this is a monthly archive */ } elseif (is_search()) { ?>
<p>You have searched the <a href="<?php echo bloginfo('home'); ?>/"><?php echo bloginfo('name'); ?></a> weblog archives
for <strong>'<?php echo wp_specialchars($s); ?>'</strong>. If you are unable to find anything in these search results, you can try one of these links.</p>
<?php /* If this is a monthly archive */ } elseif (isset($_GET['paged']) && !empty($_GET['paged'])) { ?>
<p>You are currently browsing the <a href="<?php echo bloginfo('home'); ?>/"><?php echo bloginfo('name'); ?>*lt;/a> weblog archives.</p>
<?php } ?>
</li>
<?php wp_list_pages('title_li=<h2>Pages' ); ?>
<li><h2>Archives</h2>
<ul>
<?php wp_get_archives('type=monthly'); ?>
</ul>
</li>
<li><h2>Get our Feed!</h2>
<ul>
<li><a href="http://feeds.feedburner.com/45royale/idjP" title="RSS Feed">45royale's RSS Feed</a></li>
</ul>
</li>
<?php /* If this is the frontpage */ if ( is_home() || is_page() ) { ?>
<?php get_links_list(); ?>
<li><h2>Meta</h2>
<ul>
<?php wp_register(); ?>
<li><?php wp_loginout(); ?></li>
<li><a href="http://validator.w3.org/check/referer" title="This page validates as XHTML 1.0 Transitional">Valid <abbr title="eXtensible HyperText Markup Language">XHTML</abbr></a></li>
<li><a href="http://gmpg.org/xfn/"><abbr title="XHTML Friends Network">XFN</abbr></a></li>
<li><a href="http://wordpress.org/" title="Powered by WordPress, state-of-the-art semantic personal publishing platform.">WordPress</a></li>
<?php wp_meta(); ?>
</ul>
</li>
<?php } ?>
</ul>
</div> |
Ok, so now we have a new sidebar that we’ll want to put somewhere else. A good place for this sidebar would be on the individual blog post page, no? So let’s put it there.
The implementation
Having this modified sidebar is fine and dandy, but we need to figure out how to get WordPress to put it into our individual post page. To do this, we have to edit single.php and place an extra line of code within the file. Once you have single.php open, find the line of code that calls in the footer (this is at the bottom of the page): <?php get_footer.php ?>. Assuming we still want the sidebar in the same place as on the home page and to keep the existing CSS styling, we’ll simply drop in our new line of code above the footer:
<?php include @ ("includes/alt_sidebar.php"); ?>
And that’s it. The new sidebar will be pulled into the individual posts page and the information that you’ve entered will appear. It doesn’t look too pretty, but now you have the ability to style your alternate sidebar however you choose. You could also give this sidebar’s wrapping div a new name, say <div id="alt_sidebar"></div> so that you have ultimate control over it’s layout and appearance. This technique isn’t limited to just the sidebar, or WordPress functions for that matter. You can quickly and easily add in anything you want using PHP includes (hint: the recent project box on our home page is a PHP include that we call in dynamically). So the sky’s the limit. If you have any questions or comments, as always, please leave them below. Now start using PHP includes and begin working smarter!
This is a great tutorial! Thanks for the WordPress info, we need more like it and it’s much appreciated!
Kick butt tutorial! Thanks and please continue to grace us with your ultimate knowledge!
Great article! Thanks a lot for this one!
-Peter
Where are my socks? Oh they’re way over there. OMG you knocked my socks off.
Killer tutorial.
hi mate, this code ‘highlighting’ is so unreadable, maybe try:
http://wordpress.org/extend/plugins/wp-syntax/
cheers ;)
This method of creating an alternate sidebar is not the best use of wordpress.
If you wish to define and create a second sidebar, say for example a sidebar for the right side of the page, you simply need to create a file called “sidebar-right.php” in the root of your current theme and then in the template use the following tag to include the new sidebar.
You can read more here:
Include_Tags
Ok wordpress stripped my code:
To include the new sidebar, type:
<?php get_sidebar(‘right’); ?>
I just started using wordpress and this article was very helpful.
And I get to follow you on twitter. Sweet!
Chrisgen
Is there a use for custom fields in this example? I have just started getting into them and they are really powerful. Not sure if they help in this example however.
What I am looking to do is similar. I am looking to have a static page as well as the blog appear on the main page of my wordpress site. Right now going to the settings section will only allow me to do one or the other. any feedback would be appreciated. Is there a special PHP INCLUDE tag I should be using for both to appear?