4

Sorry if the subject is a bit strange. But I didn't know what else to name it.

I would like to style this peace of code to fit with the style of my website.

<?php 
  global $post;
  $opties = wp_get_post_terms($post->ID, 'Items', array("fields" => "names"));
  if (count($opties) > 0) {
      echo implode(', ', $opties);  
  }
?>

Right now it echo's

Item 1, Item 2, Item 3, etc...

I would like to have it echo the following:

<i class="fa fa-check" aria-hidden="true"></i>Item 1<br>
<i class="fa fa-check" aria-hidden="true"></i>Item 2<br>
<i class="fa fa-check" aria-hidden="true"></i>Item 3<br>

How to I get this code to do that? Thanks in advance!

1
  • Try combining foreach and echo. Commented Jul 28, 2016 at 10:04

2 Answers 2

8

You can combine HTML easily by using the alternative syntax for foreach.

<?php 
    global $post;
    $opties = wp_get_post_terms($post->ID, 'Items', array("fields" => "names"));
    foreach ($opties as $o): ?>
        <i class="fa fa-check" aria-hidden="true"></i><?php echo $o ?></i>
    <?php endforeach; ?>
Sign up to request clarification or add additional context in comments.

Comments

5

Use foreach loop to loop through all record and display it.

<?php 
  global $post;
  $opties = wp_get_post_terms($post->ID, 'Items', array("fields" => "names"));
  foreach($opties as $opt) {
     echo '<i class="fa fa-check" aria-hidden="true"></i>'.$opt.'<br>';
  }   
?>

1 Comment

count is not required, coz foreach do nothing on empty resource ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.