Steggie Steggie - 4 months ago 7
PHP Question

How to output HTML from array in 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!

Answer

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; ?>