Author Topic: Display sidebar contents only on a particular page – WordPress  (Read 1457 times)

bbasujon

  • Administrator
  • VIP Member
  • *****
  • Posts: 1827
  • I want to show my performance at any where
    • View Profile
    • Higher Education
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(); ?>[/url]</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.



Acquire the knowledge and share the knowledge so that knowing,learning then sharing - all are the collection