Question

I am trying to dynamically insert HTML tags in wordpress. I using the code below to get all the terms in a custom taxonomy colour and it works fine:

PHP:

$terms = get_terms("colour");
 if ( !empty( $terms ) && !is_wp_error( $terms ) ){
 echo "<ul>";
 foreach ( $terms as $term ) {
   echo "<li>" . $term->name . "</li>";

 }
 echo "</ul>";
}

However I want the Html to be rendered to be as follows:

 <label><input type="checkbox" rel="Cream"/> Cream </label>
 <label><input type="checkbox" rel="White"/> White </label>
 <label><input type="checkbox" rel="Yellow"/> Yellow </label>

Please help in how i can achieve this?

Was it helpful?

Solution

$terms = get_terms("colour");
   if ( !empty( $terms ) && !is_wp_error( $terms ) ){
   $html = '';
   foreach ( $terms as $term ) {
     $html .=  '<label><input type="checkbox" rel="' . $term->name .'"/> '.$term->name.' </label>';

    }
  echo $html;
 }

OTHER TIPS

$terms = get_terms("colour");
 if ( !empty( $terms ) && !is_wp_error( $terms ) ){
 foreach ( $terms as $term ) {
   echo '<label><input type="checkbox" rel="'.$term->name.'"/> '.$term->name.' </label>';
 }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top