Jump to content

Help With a Simple IF Statement Please...


godrob

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/266635-help-with-a-simple-if-statement-please/
Share on other sites

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 } ?>

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.