Display sidebar contents only on a particular page – WordPress


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.

  1. Page.php
  2. Single.php
  3. Index.php
  4. Search.php
  5. 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.

Single Sidebar

Single Sidebar

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.

DashBoard Category Option

DashBoard Category Option

Status Bar Showing the Category ID

Status Bar Showing the Category ID

Note: Please do feel free to comment in case you face any problem adding this feature.

23 Responses

  1. Rohit Sane says:

    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 =-.

    • Ramkumar says:

      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.

  2. 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 =-.

  3. 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 =-.

    • Ramkumar says:

      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

  4. techtrickz says:

    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 =-.

    • Ramkumar says:

      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.

  5. 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 =-.

  6. Ankit says:

    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!.

    • Ramkumar says:

      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.

  7. NavaPavan says:

    Seems a better way for those who tweaks with the codes. Nice coding tutorial.

  8. Anish K.S says:

    will try it, thanks for the Tip Ramu
    .-= Anish K.S´s latest blog ..Microsoft India Empowers Your Computer to Interact in your Language =-.

  9. TechChunks says:

    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 =-.

  10. I will try this tip. Although someone said me of a plugin, I love doing it all my self!

  11. Ramkumar says:

    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 :)

  12. I will try this tip. Although someone said me of a plugin, I love doing it all my self!

  13. Ramkumar says:

    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 :)

Leave a Reply

© 2010 ByteChip – The Complete Technology Blog. All rights reserved.