我正在自学PHP,找不到与我想要做的类似的答案。如果你知道一篇文章,请把它粘贴在这里,我会查看它,或者你可以在这里帮助我。
我要做的是创建一个循环,该循环将更改get_field_object中的$field_name('$field_name')。除了$field_name和我从目录中检索的图像外,我的分区标记中的所有内容都相似。
这是我的代码:
<div class="clientele">
<section>
<img src="<?php echo get_template_directory_uri(); ?>/img/client.png" />
<h2><?php $field = get_field_object("client"); echo $field['label']; ?></h2>
<p><?php echo $field['value']; ?></p>
</section>
<section>
<img src="<?php echo get_template_directory_uri(); ?>/img/task.png" />
<h2><?php $field = get_field_object("task"); echo $field['label']; ?></h2>
<p><?php echo $field['value']; ?></p>
</section>
<section>
<img src="<?php echo get_template_directory_uri(); ?>/img/brand.png" />
<h2><?php $field = get_field_object("brand"); echo $field['label']; ?></h2>
<p><?php echo $field['value']; ?></p>
</section>
</div>
显然有更好的方法来循环这个,因为我明白我在重复自己。
您可以使用此代码
$data_array = array("client","task","brand");
$output = '<div class="clientele">';
foreach($data_array as $data){
$Field = get_field_object($data);
$output .= '<section>';
$output .= '<img src='. get_template_directory_uri()."/img/{$data}.png/>";
$output .= '<h2>'. $field['label'] . "</h2>";
$output .= "<p>" . $field['value'] . "</p>";
$output .= '</section>';
}
$output .= '</div>';
echo $output;