All posts tagged Conditional Tags

Displaying a Widget in a descendant category with Widget Logic

Overview

In this example ‘widget’ is the slug of the parent Category and ( 1 ) is the category ID number.

in_category( 'widget' ) || post_is_in_descendant_category( 1 )

I ran into an problem when a client asked me to show a 300 x 250px banner, only on a specific parent category and it’s child categories.

Step 1: Get the Category ID’s

In your WordPress Dashboard go to Posts > Categories.

edit-category

Hover over Category Title and Click "Edit."

Once you’ve clicked edit, view your URL bar, look for the number that appears after “ID=

category-id

 

Step 2: Add the Code to Widget Logic

In your WordPress Dashboard go to Appearance > Widgets

Open the appropriate widget and add the following code to

in_category( 'widget' ) || post_is_in_descendant_category( 1 )

In this example ‘widget’ is the slug of the parent category and ( 1 ) is the category ID number.

widget-logic

Further Reading:

http://wordpress.org/extend/plugins/reveal-ids-for-wp-admin-25/ (< Plugin)

http://www.wprecipes.com/how-to-find-wordpress-category-id

http://yoast.com/showing-subcategories-on-wordpress-category-pages/

WordPress Conditional Tags

The Conditional Tags can be used in your Template files to change what content is displayed and how that content is displayed on a particular page depending on what conditions that page matches. For example, you might want to display some text above the series of posts, but only on the main page of your site, with the is_home() Conditional Tag, that task is made easy.

Common Conditional Tags

is_home()
When the main blog page is being displayed.

is_front_page()
When the front of the site is displayed

is_single()
When any single Post (or attachment, or custom Post Type) page is being displayed.

is_page()
When any Page is being displayed.

is_category()
When any Category archive page is being displayed.

is_tag()
When any Tag archive page is being displayed.

is_author()
When any Author page is being displayed.

Example Conditional Code

<?php  if ( is_page( 'is_front_page' ) ) {   ?>
This Conditional Tags would only appear on your site's homepage.
<?php  } ?>