Display Widgets in WordPress based on Conditional Statements July 11, 2011Posted by Tournas Dimitrios in Wordpress - 3.
If you had never experience with programming before , probably the term will sound like mathematics . A conditional statement in math (and in programming ) is a statement in the if-then form . Conditional statements , often called conditionals for short , are used extensively in a form of logic called deductive reasoning . Students usually study conditionals and their variations in a high school geometry / programming course .
You may ask ” what has that conditional thing to do with WordPress ??? ” . Well , WordPress is a Framework that is based on the PHP programming language . Sometimes when you write programming code, you may want to perform different actions for different decisions. You can use conditional statements in your code to do this. Conditional statements are the set of commands used to perform different actions based on different conditions .
WordPress has only three different types of pages , namely , the home page , the post page and static page . Widgets can be displayed on these type of pages . Sometimes , there is the need to have different widgets on different pages . Other times , there is the need to display these widgets only if some conditions are met . Some conditionals used by WordPress are :
- is_home() –> is this your home page ?
- is_page(‘about’) –> is this the “about” page?
- is_admin() –> is this the administrator page ?
- Codex.WordPress.org has a long list of conditionals with nice code examples .
For those , that don’t like to get involved in programming and want to handle basic conditional statements for their widgets , the WordPress community provides a nice plugin . Widget Logic lets you control on which pages widgets appear. It uses any of WP’s conditional tags . This plugin gives every widget an extra control field called “Widget logic” that lets you control the pages that the widget will appear on . Once installed / enabled , every widget you put on your blog’s sidebar will now have a new field at the bottom called ” WP-Logic ” where you will include the logic that defines whether or not to display the widget . The definitions of these conditionals are based on PHP functions or WordPress Conditional tags . A full list of these conditionals can be found here .