我有一个具有两个选项的foreach循环。
“图像和文本”或“纯文本”
当用户选择“图像和文本”时,部分需要交替图像左和图像右。我可以使用css :nth-of-type实现这一点。但只有当图像和文本块是连续的时,这才有效。
我需要做的是检查用户选择了哪种类型的部分,如果是“图像和文本”,则添加一个类来定义布局,并为下一个也是“图像和文本”的部分做相反的事情。
迷惑了?我也是。为了更好地理解,这里有一些html展示了这些部分应该如何流动:
<div class="image-text left"></div>
<div class="text"></div>
<div class="image-text right"></div>这是我当前的PHP循环:
<?php
global $prefix;
$career_profiles = rwmb_meta("career_profiles");
foreach ( $career_profiles as $profile ) :
$career_section = isset( $profile["{$prefix}career_section"] ) ? $profile["{$prefix}career_section"] : '';
/**
* Careers section can be image and text OR text
* @var [type]
*/
$career_section_class = ( $career_section == 'image_text' ) ? 'image-text' : 'text';
$career_image = isset( $profile["{$prefix}career_image"] ) ? $profile["{$prefix}career_image"] : array();
foreach ( $career_image as $ci ) :
$photo = RWMB_Image_Field::file_info( $ci, array( 'size' => 'careers_photo' ) );
endforeach;
$career_name = isset( $profile["{$prefix}career_name"] ) ? $profile["{$prefix}career_name"] : '';
$career_name_id = clean($career_name);
$career_introduction = isset( $profile["{$prefix}career_introduction"] ) ? $profile["{$prefix}career_introduction"] : '';
?>
<div class="<?php echo $career_section_class; ?> clearfix">
<?php if ($photo) : ?>
<div class="image b-lazy" data-src="<?php echo $photo['url']; ?>"></div><!-- /.image -->
<?php endif; ?>
<div class="profile clearfix">
<div class="profile-wrapper font22 clearfix">
<?php if ($career_name) : ?>
<h2 class="font34"><?php echo $career_name; ?></h2>
<?php endif; ?>
<?php echo wpautop($career_introduction); ?>
</div><!-- /.profile-wrapper -->
</div><!-- /.profile -->
</div><!-- /.career-profiles_profile -->
<?php
endforeach; // foreach ( $career_profiles as $section ) :
?>我的一个想法是检查当前循环是否$career_section_class == 'image-text',然后添加left或right类,但我如何检查添加定位类的顺序?
发布于 2019-05-07 22:36:51
对于这里的其他任何人来说,解决方案是:
$align = "";
foreach ( $career_profiles as $profile ) :
$career_section = isset( $profile["{$prefix}career_section"] ) ? $profile["{$prefix}career_section"] : '';
$career_section_class = ( $career_section == 'image_text' ) ? 'image-text' : 'text';
/**
* Alternate the class if the section == image_text
*/
if ($career_section_class == 'image_text') {
$align = $align == "left" ? "right" : "left";
}
endforeach; // foreach ( $career_profiles as $section ) :https://stackoverflow.com/questions/56024370
复制相似问题