12:56 pm - Thursday February 9, 2012

Display sidebar contents only on a particular page – WordPress

  • Share
  • Share

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 Display sidebar contents only on a particular page   Wordpress

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.

category Display sidebar contents only on a particular page   Wordpress

DashBoard Category Option

category id Display sidebar contents only on a particular page   Wordpress

Status Bar Showing the Category ID

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

pixel Display sidebar contents only on a particular page   Wordpress

Loading
Filed in: Tutorials

26 Responses to “Display sidebar contents only on a particular page – WordPress”

  1. March 10, 2010 at 2:40 pm #

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

    • March 10, 2010 at 2:48 pm #

      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. March 10, 2010 at 7:21 pm #

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

    • March 10, 2010 at 8:16 pm #

      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.

  3. March 10, 2010 at 7:26 pm #

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

    • March 10, 2010 at 8:14 pm #

      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. March 10, 2010 at 7:46 pm #

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

    • March 10, 2010 at 8:13 pm #

      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. March 10, 2010 at 7:47 pm #

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

    • March 10, 2010 at 8:11 pm #

      Thanks Shiva. Glad you liked it :)

  6. March 11, 2010 at 4:52 pm #

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

    • March 11, 2010 at 6:23 pm #

      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. March 11, 2010 at 5:00 pm #

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

  8. March 12, 2010 at 10:39 am #

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

    • March 12, 2010 at 10:54 am #

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

  9. March 12, 2010 at 8:44 pm #

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

    • March 13, 2010 at 11:15 am #

      Sure to help you mate, More wordpress tips coming soon on ByteChip :)

  10. April 16, 2010 at 2:25 am #

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

    • April 16, 2010 at 4:26 am #

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

  11. April 16, 2010 at 7:55 am #

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

  12. April 16, 2010 at 9:56 am #

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

  13. December 3, 2010 at 11:37 am #

    reading tech blogs to keep me updated on technology is what i do daily. i am a tech addict :`.

  14. February 23, 2011 at 12:13 am #

    Awesome. I had been looking for something like this for a long time. Thanks a lot Ram!

  15. Gull
    August 25, 2011 at 11:30 am #

    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

Trackbacks/Pingbacks

  1. Introduction to SEO | Affiliate Success News - March 13, 2010

    [...] Show WordPress Sidebar contents only on a particular page | ByteChip – The Complete Technology… [...]

Leave a Reply