You would have come across many websites that uses the same sidebar in all its pages. For example in WordPress, you would have noticed that the same sidebar content is displayed in the following pages.
- Page.php
- Single.php
- Index.php
- Search.php
- Category.php etc
But it is not a good methodology to display the same content in sidebar over and over again. The reader must be exposed to a variety of good articles in your website, to do this you got to display more.
Another case that you justify the use of different content sidebar is that , the height of the index page will be very less than that of the single page. In that case you can see a lot of white space being left blank in the sidebar. We can actually capitalize this free space and turn it into an assert for us. Every amount of space that you leave blank can actually be modified into something very useful, provided you don’t bring down the look of the theme.
There are two ways of getting this problem solved.
Using a Different Sidebar for single.php
A simple solution, isn’t it. Just make a copy of the file “sidebar.php” and save it in some other name say “sidebar_single.php” make the changes in the file, add new contents that you would want. Now navigate to your single.php file and in the sidebar calling function, replace “sidebar.php” with “sidebar_single.php”.
Requirement:
This method will only work if the single.php uses the “INCLUDE” function to call the sidebar. However for themes that doesn’t use include method and calls the function directly, you got to follow the other method.
Using a IF condition in sidebar.php
In this method, one common sidebar php file is used for all the pages, however we decide what content should be displayed by introducing a “IF” condition. This will be the syntax for the if Condition.
<?php if (is_single( ))
{ //Statements here }
?>
Example
Say you want display 10 posts from a particular category only on the “single” page. Then you got to use the following code syntax.
<?php if (is_single( ))
{ ?>
<div id=”sidebar_box”><h3>Our Best</h3> <div> <ul>
<?php $my_query = new WP_Query(“cat=180&showposts=10″);
while ($my_query->have_posts()) : $my_query->the_post();?>
<li><a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul></div></div>
<?php } ?>
Here I have specified my category ID as 180 and Number of posts as 10, you should modify the code according to the category you want to display.
Finding the Category ID in WordPress
To find the category, since browse to Dashboard , Posts-> Categories and they move your mouse to the category text displayed in the right, that will show you the category ID, in the status bar.
Note: Please do feel free to comment in case you face any problem adding this feature.





Thanks a really cool wordpress tutorial. I have often seen people cribbing over their sidebar contents, now that can decide which one’s they want!
.-= Rohit Sane´s latest blog ..15 old HTML tags not supported by HTML 5 =-.
Thanks mate, I tried to implement this in ByteChip, got it done after a long time, so thought of making it easy for fellow bloggers.
Nice one bro
But i dont have that much knowledge in Editing PHp .
I want to try it
I think some plugins will do the same tricks
.-= sudharsan @ technoskillonline ´s latest blog ..Infolinks Minimized Payout From 100$ to 50$ – Payment Made Easy =-.
Yup there may exists some plugin mate, but why add more plugins and load the site, this is handy and could save lot of space.
So you are eliminating sidebar_single.php??
I din Understand… I guess… It wud have been great if you had put screenshot of web page how it looks… after the change is made..
.-= Chethan | IPL 2010 Tickets´s latest blog ..Book DLF IPL 2010 Tickets Online: Vodafone| Ticketgenie.in| iplt20.com =-.
Nope Im not eliminating sidebar_single.php, Im replacing sidebar.php with sidebar_single.php. I will add the before and after image for better understanding. Thanks for pointing it out
Cheers
Thanks, very useful tutorial.
I am a beginner, but I like very much in PHP coding and editing wordpress template.
.-= techtrickz´s latest blog ..Download stability and reliability update for Windows 7 =-.
Glad you liked it TechTrikz. PHP coding is really fun man, very easy and interesting
Im a BE computer student, so dint find PHP tough.
Wow, a great tutorial indeed. I will try to implements this in my blog
.-= Shiva | WP Shopping Pages Discount´s latest blog ..WP Shopping Pages Plugin – Create your own affiliate store using WordPress =-.
Thanks Shiva. Glad you liked it
Thanks for the nice tutorial brother. Going to first implement on my bro’s blog and then will try it on my blog.
Post RTd!.
Thanks for the Re-tweet mate, And BTW you dint tell me about your brother’s blog. Do share his URL so I could spend some time on his blog too. And please do feel to ask me any help in implementing this on wordpress, I will be glad to help you ,. Cheers.
Seems a better way for those who tweaks with the codes. Nice coding tutorial.
Thanks mate, Lot more wordpress tutorial yet to come on ByteChip, Do subscribe to our feeds to get our latest articles, http://feeds.feedburner.com/Bytechip
will try it, thanks for the Tip Ramu
.-= Anish K.S´s latest blog ..Microsoft India Empowers Your Computer to Interact in your Language =-.
Sure Anish, and its different to see someone calling me Ramu, as my own is the only person who used to call me that way
Awesome. I had been looking for something like this for a long time. Thanks a lot Ram!
.-= TechChunks´s latest blog ..Opera Mini 5 Beta Comes to Android =-.
Sure to help you mate, More wordpress tips coming soon on ByteChip
I will try this tip. Although someone said me of a plugin, I love doing it all my self!
The plugin can display in homepage and not in other pages, the other way round is not possible I guess. Correct me if I'm wrong
I will try this tip. Although someone said me of a plugin, I love doing it all my self!
The plugin can display in homepage and not in other pages, the other way round is not possible I guess. Correct me if I'm wrong
reading tech blogs to keep me updated on technology is what i do daily. i am a tech addict :`.
Awesome. I had been looking for something like this for a long time. Thanks a lot Ram!
hy…..i wanna display different side bar on my wordpress site…let me explain…..my few pages have the side bar displaying “blog and news”+recent posts and my few pages only display “recent posts” and few pages display only”blog and news “…………….so how i put condition on them i have register bla bla all these thing but im confused in condition and where to put it……………..thanks to every one…..in advance