Wordpress Conditional Tags In Action (Different Sidebars)


Sep 16

Wordpress can be used to create any type of site that’s because of it’s powerful functions  which give developers the freedom to play and create , this tutorial is about a simple yet great for CMS sites tip which is creating different sidebars .

Problem #1

When developing sites using wordpress you may need to create different sidebars maybe just different in content or in style too .

Solution :

We will create two sidebars one for home page , one for pages and one for blog (categories , posts , archives … etc) .

<?php if ( is_front_page() ) {?>
	<div id="home_sidebar">
	...sidebar content.....
	</div>
<?php } /* end home_sidebar */  ?>

<?php if ( is_page() && !is_front_page() ) {?>
	<div id="pages_sidebar">
	...sidebar content.....
	</div>
<?php } /* end pages_sidebar */ ?>

<?php else {?>
	<div id="blog_sidebar">
	...sidebar content.....
	</div>
<?php } /* end blog_sidebar */ ?>

the code above explains itself , we simply created our three sidebars #home_sidebar , #pages_sidebar , #blog_sidebar which will contain different content , we used Wordpress Conditional Tags ( is_front_page() , is_page() ) .

Line 1 : used is_front_page() tag within an if statement in order to display #home_sidebar content only in the front page
Note : there’s a big difference between is_front_page() and is_home()

Line 7 : used is_page() && !is_front_page() within an if statement in order to display the #pages_sidebar within all pages and used && !is_front_page() in order not to show this sidebar within the home page if you using static page as your home page

Line 13 : If not home page or page the #blog_sidebar will be displayed

Problem #2

If you used a premium portfolio or business theme or looking forward developing your own , you’ll notice that those themes looks like they using two wordpress installations , as you’ll find in most of these themes Blog and Portfolio , both with different styling , sidebars and sometimes layout

Solution :

With the power of conditional tags it’s not a problem , we just will create two different parent categories

Blog
> news
> tips
> sub category
Portfolio
> web
> print

<?php if ( is_category('Blog') && in_category('Blog')  ) {?>
	<div id="blog_sidebar">
	...sidebar content.....
	</div>
<?php } /* end blog_sidebar */ ?>

<?php if ( is_category('Portfolio') && in_category('Portfolio')  ) {?>
	<div id="portfolio_sidebar">
	...sidebar content.....
	</div>
<?php } /* end portfolio_sidebar */ ?>


Line 1 :
used is_category(’Blog’) && in_category(’Blog’) within if statement to display #blog_sidebar only within category Blog , it’s sub-categories and post pages within the Blog category and it’s sub-categories .

Line 1 : used is_category(’Portfolio’) && in_category(’Portfolio’) within if statement to display #portfolio_sidebar only within category Portfolio , it’s sub-categories and post pages within the Portfolio category and it’s sub-categories .

… solved

  • Share/Bookmark


2 Comments On This Post


Trackbacks

  1. Wordpress Conditional Tags In Action (Different Sidebars) | ShareFavorite
  2. Wordpress Conditional Tags In Action (Different Sidebars) | WP Citrus

Leave Your Own Comment on This Post

Spam protection by WP Captcha-Free




Copyright © 2010 Banhawi