Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

How to show WordPress menu on specific page

Hello.

I'm working on a Genesis WordPress site that has four menus (www.raccoon.shop). The third menu which has big circle images must be shown only on the front homepage. The fourth menu must be shown on the same template place as the third menu but it will be active on ALL OTHER pages except the homepage.

My PHP knowledge is very limited. What do I need to write in my function.php file to make third menu active only on homepage and fourth menu active only on all pages except the homepage?

Thank you!

1 Answer

Sue Dough
Sue Dough
35,800 Points

Use the if_home function to check if you are on the home page and then run your code.

<?php

function custom_menu_treehouse() {
  if ( is_home() ) {
    wp_nav_menu( array( 'menu' => 'menu3'  ) );
  } else {
    wp_nav_menu( array( 'menu' => 'menu4'  ) );
  }
}


?>

Sources: https://developer.wordpress.org/reference/functions/is_home/ https://developer.wordpress.org/reference/functions/wp_nav_menu/ http://stackoverflow.com/questions/7816282/wordpress-specific-menus-for-specific-page

wp_nav_menu( array( 'menu' => 'menu3'  ) );

Hmm, looks simple :D. The first 'menu' is the place of the menu and the 'menu3' is the menu id?

Sue Dough
Sue Dough
35,800 Points

'menu' should not be changed. menu3 would be the name of the menu. The names of the menu can vary. Check the docs and try it out.