• Resolved jonoc73

    (@jonoc73)


    Hi,

    I use the Custom Field Template plugin and it works great.

    I need to use some checkboxes to display a number of guides on a trip. I use the following to echo data input from the custom field:

    <?php  if((get_post_meta($post->ID, "guides",  true))) { ?>
    Guides on this tour: <?php echo get_post_meta($post->ID, 'guides',  TRUE); ?><?php } ?>

    But instead of displaying all the names selected by a checkbox button, it just lists the first one. What am I doing wrong?

    Thanks Jono

Viewing 7 replies - 1 through 7 (of 7 total)
  • 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”

    Thread Starter jonoc73

    (@jonoc73)

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

    Thread Starter jonoc73

    (@jonoc73)

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

    https://us3.php.net

    Thread Starter jonoc73

    (@jonoc73)

    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

    Thread Starter jonoc73

    (@jonoc73)

    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

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Displaying Custom Field checkbox’ is closed to new replies.