I'm trying to convert wordpress tags (and other input) to html classes. First I query the posts, set them in a while loop and in this while loop I convert the tags to usefull classes. I've got this right now:
<?php while ($query->have_posts()) : $query->the_post();
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
$thetags = $tag->name . '';
echo $the_tags;
$thetags = strtolower($thetags);
$thetags = str_replace(' ','-',$thetags);
echo $thetags;
}
}
?>
<!-- Loop posts -->
<li class="item <?php echo $thetags ?>" id="<?php the_ID(); ?>" data-permalink="<?php the_permalink(); ?>">
<?php endwhile; ?>
Now what's the problem:
The first echo, echoes the tags like: Tag 1 Tag 2. The second echoes it like tag-1tag-2, what is not what I want either because there are no spaces between every tag. Thereby is only the last tag shown in the html class, because it's not in the foreach loop.
What do I want: I want to have all related tags in the html class. So the end result must be like:
<li class="item tag-1 tag-2 tag-4" id="32" data-permalink="thelink">
However if I would put the list item in the foreach loop, I would get a <li>
item for every tag. How to do this properly? Thanks!