我有一个 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/