WordPress Conditional Tags

  • Nil 

WordPress Conditional Tag is a special function that checks whether a particular condition is matched or not and returns true or false. It is commonly used with PHP if /else Conditional Statements to alter the display of content.

For example: To alter exclude a certain category [category id: 5] in the main query, we check whether this is the main query or not and alter the WordPress query to exclude that category.

add_action( 'pre_get_posts', 'foo_modify_query_exclude_category' );
function foo_modify_query_exclude_category( $query ) {
if ( ! is_admin() && $query->is_main_query() && ! $query->get( 'cat' ))
        $query->set( 'cat', '-5' );
}

Reference: Conditional Tags | Theme Developer Handbook