1

I'm using the advanced custom field plugin for wordpress and wondering if I'm able to do the following:

If checkbox ticked = web and it equals the page title echo this.

Does anyone know what code to use for this?

UPDATE: The closest I've got is this:

<?php $fields = get_acf(); print_r ($fields->categories); ?> 

But that is printing out this:

Array ( [0] => option_1 [1] => option_2 [2] => option_9 )

1. How can I print out the real names (without it saying array)?

2 Also say if it = web and matches the page title then echo something?

1

The way advanced custom field plugin handles checkboxes is in an array so you work with that array, for example if you field name is categories and you want to see if web (on of the checkboxes) was check you can use:

<?php if(in_array('web', get_field('categories') )): ?>
    <h1>Web was ticked!</h1>
<?php endif; ?>

To answer your questions:

  1. How can I print out the real names (without it saying array)?

if you want to print out the names instead of the array you can use:

$cats = get_field('categories');
echo '<ul>';
foreach ($cats as $key => $val){
    echo '<li>'.$val.'</li>';
}
echo '<ul>';

this should give you something like:

  • option_1
  • option_2
  • option_9

2 Also say if it = web and matches the page title then echo something?

just like the first code snippet but just add another condition:

<?php if(in_array('web', get_field('categories') ) && $post->post_title == "web"){
     echo  '<h1>Web was ticked! and matches the page title</h1>
}
  • Thanks very much for the comprehensive answer. The one bit that didn't quite work (due to me not explaining well enough!!) was the real names. For the checkbox setting's in the advanced custom field I've got it setup like this (one per line) "branding : Branding, web: Web, print : Print". It's still coming up as option_1, option_2 etc. – Rob May 16 '11 at 14:55
  • Also, one thing I forgot to add was: How can I display all the ticked checkboxes and output them one by one so I can give them a link? – Rob May 16 '11 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.