0

wordpress タグ (およびその他の入力) を html クラスに変換しようとしています。最初に投稿をクエリし、while ループで設定し、この while ループでタグを便利なクラスに変換します。私は今これを持っています:

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

今問題は何ですか:

最初のエコーは、タグ 1 タグ 2 のようにタグをエコーし​​ます。これは、foreach ループ内にないため、html クラスに表示される最後のタグのみです。

何が必要ですか: 関連するすべてのタグを html クラスに含めたいです。したがって、最終結果は次のようになります。

<li class="item tag-1 tag-2 tag-4" id="32" data-permalink="thelink">

ただし、リスト項目を foreach ループに入れると、<li>すべてのタグの項目が取得されます。これを適切に行う方法は?ありがとう!

4

2 に答える 2

1

私はこのようなことをします(代わりに配列を使用してから、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(); ?>">
于 2013-09-25T13:09:44.493 に答える