How To: Sidebar.php

I’ve had a few people ask me about my sidebar and how I managed to get the recent articles and comments to be viewable for every page. Since PHP is just like every other language on the planet, if you can understand the basics then you can do more than just hack it up like I’m about to show you. First let’s cover the article plug on every page.

From your root, go find the directory where K2 is stored which should be under root\wp-content\themes. Once you are in the K2 theme, open up the file sidebar.php. This file contains all the calls to generate your secondary column or sidebar.If you just glance over the file a few things might stick out such as “sb-search”, “sb-about”, “blc_latest_comments”, “sb-asides” and then “sb-latest”. This “sb-latest” will be the chunk of code that we will be worrying about first.

The first php call that starts with < ?php if ( (is_home())... is a huge if/or statement that determines what page you are currently viewing and whether or not to display this particular section of the sidebar. If you want this to be visible on every page, you can simple remove this entire section and the closing < ?php)?> comment that follows the < /div>. Your code should look something like this.


<div class="sb-latest"><h2><?php _e('Latest Articles'); ?></h2>
<span class="metalink">< a href="http://feeds.feedburner.com/Gomeler" title="RSS Feed for Blog Entries" class="feedlink">< img src="<?php bloginfo('template_directory');?>/images/feed.png" alt="RSS"/></a>< /span>
<ul>
<?php wp_get_archives('type=postbypost&limit=10'); ?>
</ul>
</div>

Now if you want, you can move this chunk of code around to where ever you want it. Now you may notice that I also have the latest comments on every page also. To get this to work you must download the Wordpress plugin Brian’s Latest Comments which I found here. Once you have this plugin activated, go back into sidebar.php and look at the code that includes “blc_latest_comments”. Within the first line you will notice another if statement that checks if the blc_latest comments function exists and if you are viewing the homepage. Since we want these comments to be on every page, just remove the “&& is_home()” from this line. There you go, now you have your latest comments on every page. Your code should look similar to this.


<? php if ((function_exists('blc_latest_comments'))) { ?>
<div class="sb-comments"><h2><?php _e('Latest Comments');></h2>
<a href="<?php bloginfo('comments_rss2_url');>" title="RSS Feed for all Comments" class="feedlink"><img src="<?php bloginfo('template_directory'); ?>/images/feed.png" alt="RSS" />< /a>
<ul>
<?php blc_latest_comments('5','3','false'); ?>
</ul>
</div>
<?php } ?>

There you have it, now both comments and articles will show up on your sidebar. You can move the chunks of code around to your hearts desire but make sure you move the entire chunk of code. If you have any other questions on how I did stuff let me know.

The Conversation {4 comments}

  1. Chris Morrell {Monday June 19, 2006 @ 5:15 am}

    I worked on making the code look a bit better by using some HTML instead of regular characters and it pulled the code together. Now all the spacing issues are solely due to WP being a pain in the butt.

  2. Trey Copeland {Monday June 19, 2006 @ 6:58 pm}

    nice little tutorial you got here bud….

  3. Chris Morrell {Monday June 19, 2006 @ 7:29 pm}

    Thanks, I tried to keep it as simple and focused as possible which is rather hard with me. I think I managed to keep the rambling to a minimium though.

  4. Norman Freeman {Tuesday October 24, 2006 @ 1:06 pm}

    Thanks to you, but I think today it#s not neccessary to use Brian\’s solution anymore, right?

    Have a great day,
    Norman

Speak Your Mind

  • Comment Policy: No flaming unless directed at the author for good reason. All other flaming will be turned into a comment about ponies and rollerskates.