html - 从数组中拉取数据到 <div>

标签 html css arrays

我有一个 wordpress 网站,我在其中尝试使用数组将 css 类应用于元素。

我的数组如下:

<?php

$classes = array(
    0 => 'highlight-color-1',
    1 => 'highlight-color-2',
    2 => 'highlight-color-3',
    3 => 'highlight-color-4');

?>

然后我在四个不同的元素上有以下 html:

<div class="q element element--content ******">

我需要遍历数组并应用 css 类来替换 ***** 以便我得到类似这样的东西

<div class="q element element--content highlight-color-4"></div>
<div class="q element element--content highlight-color-3"></div>
<div class="q element element--content highlight-color-2"></div>
<div class="q element element--content highlight-color-1"></div>

上下文代码部分

// Displays Post Detail

if( $WP_Query->post_count > 0) :?>



<ul class="faq">

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

    $classes = array(

    0 => 'highlight-color-1',
    1 => 'highlight-color-2',
    2 => 'highlight-color-3',
    3 => 'highlight-color-4');      

    ?>

    <div class="q element element--content ">

    <div class="faq_plus"><img src="http://drbarbaramariposa.com/wp-content/plugins/accordion-faq-plugin/plus.png" ></div>

    <div class="faq_title"><?php the_title(); ?></div>

    </div>

    <li class="a">

        <?php if ( function_exists('has_post_thumbnail') && has_post_thumbnail() ) {

                the_post_thumbnail('thumbnail'); 

              }

        ?>

        <?php the_content(); ?>

    </li>

<?php $i++; endwhile; ?>

</ul>

<?php endif; ?>

非常感谢任何帮助。

克里斯

最佳答案

您可以使用 str_replace()在 foreach 循环中。

foreach ($classes as $c){
    echo str_replace("******",$c,"<div class='q element element--content ******'></div>");
}

关于html - 从数组中拉取数据到 <div>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37132514/

相关文章:

c++ - 数组中连续元素的总和,C++

html - 如何在屏幕底部正确放置一个 div?

html - AngularJS 中的 md-content 滚动问题

javascript - OnClick JS If 语句

html - 响应图像上的居中按钮

css - 使用 nth-child 的样式来保持表格的方面(交替行颜色)更新

css - 为内联 svg 添加没有文本的下划线文本装饰

javascript - 循环数组与循环对象的速度

C函数指针既不是函数也不是指针

javascript - JavaScript 中的 PHP