用PHP生成引导列
我正在尝试使用高级自定义字段为Wordpress主题创建一个引导布局。我现在已经设置好了,所以如果你有两个内容字段,它会生成一个“6”宽的列。如果有3个字段,则会生成一个“4”字段列用PHP生成引导列,php,wordpress,twitter-bootstrap,advanced-custom-fields,Php,Wordpress,Twitter Bootstrap,Advanced Custom Fields,我正在尝试使用高级自定义字段为Wordpress主题创建一个引导布局。我现在已经设置好了,所以如果你有两个内容字段,它会生成一个“6”宽的列。如果有3个字段,则会生成一个“4”字段列 <?php if(get_field('link_tiles')) : while (has_sub_field('link_tiles')) : $number_of_cols = count(get_field( 'link_tiles' )); if ($number_of_cols == 2)
<?php if(get_field('link_tiles')) :
while (has_sub_field('link_tiles')) :
$number_of_cols = count(get_field( 'link_tiles' ));
if ($number_of_cols == 2) {
echo '<div class="col-md-6" style="padding: 0; margin: 0;">';
}
elseif ($number_of_cols >= 3) {
echo '<div class="col-md-4" style="padding: 0; margin: 0;">';
}
?>
我现在真正想要的是,如果你输入了4个字段,它会生成3x“4”宽的列,然后1x“6”宽的列,如果你输入了6个字段,它会返回到生成“4”宽的列。我想使用While循环需要一些逻辑,但我不能完全理解它的逻辑。我是PHP新手,任何帮助都将不胜感激 很难说没有看到周围的代码,但您必须已经遍历了这些列(每个列都回显一个
)。您正在迭代数组吗?是否已为当前索引设置了变量
如果是这样的话,逻辑可能是这样的,假设$index等于当前列号:
// set default for two columns
$col_class = 'col-md-6';
if ($number_of_cols == 3) {
$col_class = 'col-md-4';
} elseif ($number_of_cols == 4 && $index < 4) {
$col_class = 'col-md-4';
} elseif ($number_of_cols == 4 && $index == 4) {
$col_class = 'col-md-6';
}
echo '<div class="' . $col_class . '">';
我使用的是高级自定义字段Repeater字段插件,因此为每个字段添加一个新列。我将更新第一篇文章。那么你是否使用了这里列出的基本循环?没错,是的。它计算创建的字段行数。谢谢Benjarwar,这正是我想要的!
<?php if(get_field('link_tiles')) :
$number_of_cols = count(get_field( 'link_tiles' ));
$counter = 1;
while (has_sub_field('link_tiles')) :
// set default for two columns
$col_class = 'col-md-6';
if ($number_of_cols == 3) {
$col_class = 'col-md-4';
} elseif ($number_of_cols == 4 && $counter < 4) {
$col_class = 'col-md-4';
} elseif ($number_of_cols == 4 && $counter == 4) {
$col_class = 'col-md-6';
}
echo '<div class="' . $col_class . '"></div>';
// increment the counter for the next column
$counter++;
endwhile;
endif; ?>