title: A foreach loop in a while loop
body: <p>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:</p>
  
  <pre><code> &lt;?php while ($query-&gt;have_posts()) : $query-&gt;the_post(); 
  
  
      $posttags = get_the_tags();
      if ($posttags) {
        foreach($posttags as $tag) {
          $thetags =  $tag-&gt;name . ''; 
          echo $the_tags;
  
          $thetags = strtolower($thetags);
  
  
          $thetags = str_replace(' ','-',$thetags);
          echo $thetags;
  
  
        }
     }
      ?&gt;
  
      &lt;!-- Loop posts --&gt;         
      &lt;li class="item &lt;?php echo $thetags ?&gt;" id="&lt;?php the_ID(); ?&gt;" data-permalink="&lt;?php the_permalink(); ?&gt;"&gt;
  
  &lt;?php endwhile; ?&gt;
  </code></pre>
  
  <p><strong>Now what's the problem:</strong></p>
  
  <p>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.</p>
  
  <p><strong>What do I want:</strong>
  I want to have all related tags in the html class. So the end result must be like:</p>
  
  <pre><code>&lt;li class="item tag-1 tag-2 tag-4" id="32" data-permalink="thelink"&gt;
  </code></pre>
  
  <p>However if I would put the list item in the foreach loop, I would get a <code>&lt;li&gt;</code> item for every tag. How to do this properly? Thanks!</p>