Hiding Part of a WordPress Template Based on the Type of Template Being Used

December 3, 2012

Here is a nifty piece of code you can use in the case you need to hide something or show something on a wordpress template that you have assigned to your page.

So in the instance you needed to hide the navigation menu on a certain type of page within your site you would literally add a few lines of code as the wordpress codex already provides a function for determining if the page is using a certain type of template. (see: is_page_template )

Lets assume the following.

  1. You have made a wordpress template called “template-no-nav.php” inside the theme root
  2. You have assigned your page the custom template
  3. You want to hide the navigation menu from the template

Just add the following code to the header.php file where your navigation menu would normally appear.

1
2
3
4
56
<?php
    if ( is_page_template('template-no-nav.php') ) {
    } else  {
    ?>
    <?php wp_nav_menu( array( 'theme_location' => 'primary' ) ); ?><?php } ?>