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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.