Jump Menu : Content | Explore | Comments | Search | Home | Sitemap | Contact | Login | Access.

Conditionally Load WordPress Pages

Need to load a WordPress page conditionally? For example, perhaps you need a “special” page to appear for search results? Yes? This simple PHP/JavaScript solution may be just what the doctor ordered!

Simply replace “condition” with the required condition (or delete the if (condition) {} qualifier entirely), and then change the path and file names to suit your specific needs:

<?php
if (condition) {
   echo ("
   <script type=\"text/javascript\">
   <!--//--><![CDATA[//><!--

   parent.location=\"http://www.domain.com/path/to/file.html\"

   //--><!]]>
   </script>
   ");
} else {
   echo ("
   <script type=\"text/javascript\">
   <!--//--><![CDATA[//><!--

   parent.location=\"http://www.domain.com/path/to/other-file.html\"

   //--><!]]>
   </script>
   ");
}
?>

Specifically, I employ the following trickery in my Killer WordPress theme:

<?php endwhile; else: ?>
<?php echo ("
   <script type=\"text/javascript\">
   <!--//--><![CDATA[//><!--

   parent.location=\"http://perishablepress.com/press/?p=58\"

   //--><!]]>
   </script>
"); ?>
<?php endif; ?>

The previous code chunk returns a specially designed search results page that is specifically tailored to suit the Killer theme. Works wonders.

Related articles

About this article

This is article #185, posted by Jeff Starr on Sunday, August 27, 2006 @ 05:59pm. Categorized as Function, WordPress, and tagged with javascript, search, tips, tricks, WordPress. Updated on November 04, 2007. Visited 13194 times. 0 Responses »

BookmarkSubscribeExplore

« Execute External WordPress Functions • Up • CSS Hack Dumpster »


[ Comments are closed for this post. ]


Set CSS to lite theme
Set CSS to dark theme