WordPress: How to Fix Conditional Tags Problem

August 9th, 2011 by Tony de Jesus

In WordPress you can use Conditional Tags in your Template files in order to change the way of displaying content and what content is displayed on a particular page. A very common example is if you want to display a snippet of text but only on the main page of your blog, you can use is_home() Conditional Tag. However, sometimes it seems that these tags stop working. In this post I’ll show you how to fix this problem.

In my case, I want to display different content on the sidebar based on what page the reader is. But, for some reason, the content was always the same in all pages, even by using Conditional Tags. After searching about this issue, I found the solution in the WordPress Codex. There they said that sometimes queries performed in other templates such as sidebar.php may corrupt certain conditional tags. And the solution for this is to put wp_reset_query() before the conditional test. For example:

<?php

wp_reset_query();
if ( is_home( ) ) {
     echo 'You are on the homepage!';
}

?>

,

 
 
 

Leave a Reply