You will want to use
$single=false;
get_post_meta($post->ID, "guides", $single);
as the get_post_meta article says, “If $single is set to false, or left blank, the function returns an array containing all values of the specified key”
Thanks Michael, I have probably done this wrong, but put the above into the code and it just returns the word Array instead of the tour guides names.
<?php $single=false; echo get_post_meta($post->ID, "guides", $single); ?>
Thanks Jono.
Got to cycle through the returned array
$guides=get_post_meta($post->ID, "guides", false);
if ($guides) {
foreach($guides as $guide) {
echo $guide;
}
}
That’s great – thanks for your help it works, but it’s not too pretty though. How would I add a space and a comma into the code to get something like: John, Ben, Claire
Also am I able to do something with each value. Say I used the if statement shown in the first post above, if the value is John do this and if the value is Ben do that?
Thanks Jonathan
You should be able to take this and run with it:
<?php
$guides=get_post_meta($post->ID, "guides", false);
$out='';
$between =', ';
if ($guides) {
foreach($guides as $guide) {
if ('John' == $guide ) {
$out .= $guide . ' is a man' . $between;
} elseif ('Mary' == $guide ) {
$out .= $guide . ' is a woman' . $between;
} else {
$out .= $guide . $between;
}
}
echo substr($out,0,-2); //output all but the last ', '
}
?>
http://us3.php.net
Michael, You’re helping loads – thanks once again.
When I asked if I was able to do something with each value I meant doing something like including a template path for example.
I should have explained it more sorry.
If the value is John I want to include a template where I can include his photo, contact info etc.
<?php } else { include(TEMPLATEPATH . '/guides/john.php'); } ?>
If the value includes Mary I want to include a template where I can include her photo, contact info etc.
<?php } else { include(TEMPLATEPATH . '/guides/mary.php'); } ?>
Thanks for your help so far – it’s been great.
Jonathan
I took a look at the code again and it’s simple really isn’t it!
I replace
if ('John' == $guide )
{$out .= $guide . ' is a man' . $between;}
with
if ('John' == $guide )
{include(TEMPLATEPATH . '/guides/john.php'); }
Thanks for your help.
Jonathan