Page 1 of 1

PHP If Else correction for charity website

Posted: Wed Apr 04, 2012 5:01 am
by Martin Dickson
Hi there,

Thanks for having a look at my question. I am a newbie but I have managed to built a site for my charity (see www.youthheroes.org.uk) and I want to display a different menu depending on the the location of the project we are working on. I have managed to get close to what I want using the following code which calls a Wordpress menu:

<?php if ( get_post_meta($post->ID, 'main', true) ) { ?>
<?php do_action('wp_menubar','Main Menu'); ?>
<?php } ?>

<?php if ( get_post_meta($post->ID, 'wirral', true) ) { ?>
<?php do_action('wp_menubar','Main Menu'); ?>
<?php } ?>

<?php if ( get_post_meta($post->ID, 'salford', true) ) { ?>
<?php do_action('wp_menubar','Salford'); ?>
<?php } ?>

However, I would like to include a catch all and tidy up the code a bit to make expansion easier. I have this code below but it's incorrect and returns an error :banghead: :

<?php if ( get_post_meta($post->ID, 'salford', true) ) { ?>
<?php do_action('wp_menubar','salford'); ?>
<?php elseif ( get_post_meta($post->ID, 'wirral', true) ) ?>
<?php do_action('wp_menubar','wirral'); ?>
<?php else ?>
<?php do_action('wp_menubar','Main Menu'); } ?>

Any help much appreciated.

Thanks,

Martin

Re: PHP If Else correction for charity website

Posted: Wed Apr 04, 2012 5:23 am
by litebearer
Mighth consider using a SWITCH statment

Re: PHP If Else correction for charity website

Posted: Wed Apr 04, 2012 5:29 am
by azycraze
what is the need of increasing php tags if you can just include your code in one.

<?php
if ( get_post_meta($post->ID, 'salford', true) )
{
do_action('wp_menubar','salford');
}
elseif ( get_post_meta($post->ID, 'wirral', true) )
{
do_action('wp_menubar','wirral');
}
else
{
do_action('wp_menubar','Main Menu');
}

?>

will work.

Re: PHP If Else correction for charity website

Posted: Wed Apr 04, 2012 5:32 am
by social_experiment
There are 2 options you can use but could you paste the error that you are receiving
http://php.net/manual/en/control-structures.elseif.php

Re: PHP If Else correction for charity website

Posted: Wed Apr 04, 2012 6:22 am
by Martin Dickson
Thanks all! Much appreciated.

I'll look in to those other options litebearer and social_experiment; in the meantime Azycraze has solved my problem - I messed up the { and } locations and I had no idea that I could have it all in one PHP tag - two lessons learned today!

Re: PHP If Else correction for charity website

Posted: Wed Apr 04, 2012 7:28 am
by Martin Dickson
I wonder if I can get one other quick answer to a similar thing. This works:

<?php if ( get_post_meta($post->ID, 'main', true) ) { ?>
<div class="main_table">
<?php } ?>

<?php if ( get_post_meta($post->ID, 'wirral', true) ) { ?>
<div class="main_table_wirral">
<?php } ?>

<?php if ( get_post_meta($post->ID, 'salford', true) ) { ?>
<div class="main_table_salford">
<?php } ?>

But I would like to use something like this because it allows for a default using the else:

<?php if ( get_post_meta($post->ID, 'salford', true) ) {
'<div class="main_table_salford">' ; }
elseif ( get_post_meta($post->ID, 'wirral', true) ) {
'<div class="main_table_wirral">' ; }
else {
'<div class="main_table">' ; }
?>

There are no errors in the code but the ' before and after the div class means that it just ignores this line. Without the ' I get an error presumably because I have < and > in the middle of the php.

Does that make any sense at all? I am such a novice but I am really enjoying this.

Cheers,

Martin

Re: PHP If Else correction for charity website

Posted: Wed Apr 04, 2012 7:37 am
by social_experiment
Have a look at a switch statement as litebearer suggested http://hu.php.net/manual/en/control-str ... switch.php

Re: PHP If Else correction for charity website

Posted: Thu Apr 05, 2012 12:16 am
by azycraze
use echo to output div tags
===============================
echo '<div class="main_table_salford">' ;