Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to output HTML from array in PHP

Tags:

loops

foreach

php

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!

like image 365
Steggie Avatar asked Dec 11 '22 15:12

Steggie


1 Answers

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; ?>
like image 102
Daniel Waghorn Avatar answered Dec 27 '22 05:12

Daniel Waghorn