简体   繁体   English

while循环中的foreach循环

[英]A foreach loop in a while loop

I'm trying to convert wordpress tags (and other input) to html classes. 我正在尝试将wordpress标签(和其他输入)转换为html类。 First I query the posts, set them in a while loop and in this while loop I convert the tags to usefull classes. 首先我查询帖子,在while循环中设置它们,在这个while循环中我将标签转换为有用的类。 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. 第一个回声,回应标签,如:标签1标签2.第二个回应它像tag-1tag-2,什么不是我想要的,因为每个标签之间没有空格。 Thereby is only the last tag shown in the html class, because it's not in the foreach loop. 因此它只是html类中显示的最后一个标记,因为它不在foreach循环中。

What do I want: I want to have all related tags in the html class. 我想要什么:我想在html类中拥有所有相关的标签。 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. 但是如果我将列表项放在foreach循环中,我会得到每个标记的<li>项。 How to do this properly? 怎么做得好? Thanks! 谢谢!

Use an array instead, and then implode it. 改为使用数组,然后将其内implode Do yourself a favour and use brackets in your while clause instead (if you prefer it for readability - I know I do in this case): 做一个忙,并在你的while子句中使用括号(如果你更喜欢它的可读性 - 我知道在这种情况下我做):

<?php
    while ($query->have_posts()) {
        $query->the_post(); 

        $posttags = get_the_tags();

        $tags = array(); //initiate it
        if ($posttags) {
            foreach($posttags as $tag) {
                $tags[] = str_replace(' ','-', strtolower($tag->name)); //Push it to the array
            }
        }
        ?>
            <li class="item<?php echo (!empty($tags) ? ' ' . implode(' ', $tags) : '') ?>" id="<?php the_ID(); ?>" data-permalink="<?php the_permalink(); ?>">
        <?php
    }
?>

i Would do something like this (use a array instead of that and then use implode to get it with spaces between it :) 我会做这样的事情(使用数组而不是那个然后使用implode来获取它之间的空格:)

<?php while ($query->have_posts()) : $query->the_post(); 

$tags = array(); // a array for the tags :)
$posttags = get_the_tags();
if (!empty($posttags)) {
  foreach($posttags as $tag) {
    $thetags =  $tag->name . ''; 
    echo $the_tags;

    $thetags = strtolower($thetags);


    $thetags = str_replace(' ','-',$thetags);
    $tags[] = $thetags;

    echo $thetags;


  }
}
?>

<!-- Loop posts -->      
<li class="item <?= implode(" ", $tags) ?>" id="<?php the_ID(); ?>" data-permalink="<?php the_permalink(); ?>">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM