2

I can populate selectbox options like this:

    <?php 
        $args = array(
            'taxonomy' => array(
                'country',
            ),
            'hide_empty' => 0,
            'name' => 'country'
        );
    ?>
    <?php wp_dropdown_categories($args); ?>

But the problem is how to make a checkbox list instead of a dropdown?

I mean something like this:

enter image description here

1
$taxonomy     = 'training_cats';
 $orderby      = 'name'; 
 $show_count   = 0;      // 1 for yes, 0 for no
 $pad_counts   = 0;      // 1 for yes, 0 for no
 $hierarchical = 0;      // 1 for yes, 0 for no
 $title        = '';
 $empty        = 0;
 $args = array(
  'name'     => $taxonomy,
  'orderby'      => $orderby,
  'show_count'   => $show_count,
  'pad_counts'   => $pad_counts,
  'hierarchical' => $hierarchical,
  'title_li'     => $title,
  'hide_empty'   => $empty
);
$cats = get_taxonomies($args);
foreach ( $cats as $cats2 ) {
 echo '<input type="checkbox" name="checkbox_id" value="'.$cats2->name.'" />';
 echo $cats2-name;
}

Not tested but should work, change the taxonomy name etc :)

Edit: Try the new code, using get_taxonomies (the code didn't know where to pull data from)

Edit2: Changed taxonomy to name:

8
  • Thanks, but for some reason the checkboxes are 1 more than it should. Eg. I have 6 tags and 7 checkboxes are shown. The second problem is that $cats2->name shows nothing ;(. Any idea how to fix it? – Derfder Jul 16 '13 at 10:33
  • And if I print_r($cats2) it shows post_tag instad of an array or object. I guess I need to get the taxonomy tags into the array $cats first, like the function wp_dropdown_categories($args) is doing. Any idea how t odo that? – Derfder Jul 16 '13 at 10:37
  • just edited my code, see if that works – Gareth Gillman Jul 16 '13 at 10:42
  • Hmm, I get an empty array now ;( Array ( ). Probably the get_taxonomies function has different arguments or order of them? Any idea? – Derfder Jul 16 '13 at 10:46
  • another edit done, instead of calling taxonomy use name (line 9) – Gareth Gillman Jul 16 '13 at 10:56

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.