godrob Posted August 3, 2012 Share Posted August 3, 2012 Hi guys, I'm pretty new to PHP and could really do with some help please. All I want to do is wrap the code below in a simple IF statement. Basically it control the menu code which I want to hide on a certain page. <ul class="sf-menu"> <?php if ( has_nav_menu( 'primary-menu' ) ) { ?> <?php wp_nav_menu( array( 'container' => false, 'menu_id' => 'nav', 'theme_location' => 'primary-menu', 'items_wrap' => '%3$s' ) ); ?> <?php } else { ?> <?php wp_list_pages( 'title_li=&depth=3' . bp_dtheme_page_on_front() ); ?> <?php } ?> </ul> So I tried doing something like this: <?php if (is_page('Contact')) { // Disable Main Menu } else { <ul class="sf-menu"> <?php if ( has_nav_menu( 'primary-menu' ) ) { ?> <?php wp_nav_menu( array( 'container' => false, 'menu_id' => 'nav', 'theme_location' => 'primary-menu', 'items_wrap' => '%3$s' ) ); ?> <?php } else { ?> <?php wp_list_pages( 'title_li=&depth=3' . bp_dtheme_page_on_front() ); ?> <?php } ?> </ul> } ?> ...But I get an error: Parse error: syntax error, unexpected '<' on this line <ul class="sf-menu"> Anyone out there tell me what I'm doing wrong please? Many Thanks Rob Quote Link to comment https://forums.phpfreaks.com/topic/266635-help-with-a-simple-if-statement-please/ Share on other sites More sharing options...
jazzman1 Posted August 3, 2012 Share Posted August 3, 2012 You have two errors. Do you use any IDE ? <?php if (is_page('Contact')) { // Disable Main Menu } else { ?> <ul class="sf-menu"> <?php if (has_nav_menu('primary-menu')) { ?> <?php wp_nav_menu(array('container' => false, 'menu_id' => 'nav', 'theme_location' => 'primary-menu', 'items_wrap' => '%3$s')); ?> <?php } else { ?> <?php wp_list_pages('title_li=&depth=3' . bp_dtheme_page_on_front()); ?> <?php } ?> </ul> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/266635-help-with-a-simple-if-statement-please/#findComment-1366543 Share on other sites More sharing options...
godrob Posted August 3, 2012 Author Share Posted August 3, 2012 I just worked it out after you posted. Thanks anyway for taking the time to reply - appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/266635-help-with-a-simple-if-statement-please/#findComment-1366544 Share on other sites More sharing options...
Christian F. Posted August 6, 2012 Share Posted August 6, 2012 This is how I'd do it, if I couldn't get the WP functions to return the content instead of printing it outright. <?php // Check if you're not on the contact page. if (!is_page ('Contact')) { // Since we're not, create the menu. echo '<ul class="sf-menu">'; if (has_nav_menu ('primary-menu')) { wp_nav_menu (array ('container' => false, 'menu_id' => 'nav', 'theme_location' => 'primary-menu', 'items_wrap' => '%3$s')); } else { wp_list_pages ('title_li=&depth=3' . bp_dtheme_page_on_front ()); } echo "</ul>\n"; } ?> No need to keep opening and closing the PHP tags for each line of code. It's not only very messy, but with enough PHP tags/echo statements you'll get a noticeable performance loss. Quote Link to comment https://forums.phpfreaks.com/topic/266635-help-with-a-simple-if-statement-please/#findComment-1367113 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.