我正在使用高级自定义字段在我的投资组合网站上创建交互式简历。在技能部分,我分为三类。开发、设计和工具。我在其中的每一个上使用一个转发器自定义字段来传递 4 个包含有关该技能的信息的子字段。
我将这些类别中的每一个分为 3 个中继器字段,以便于在管理部分填写。但是这三个循环本质上是一样的。他们每个人都是这个样子。
<h3>Development:</h3>
<?php // check if the repeater field has rows of data
if( have_rows('development') ):
// loop through the rows of data
while ( have_rows('development') ) : the_row();
// display a sub field value inside a card
?>
<?php
// VARIABLES //
$title = get_sub_field('title');
$logo = get_sub_field('logo');
$backImage = get_sub_field('background_image');
$overlay = get_sub_field('overlay_gradient');
$url = get_sub_field('url_link');
// THE SKILL BLOCK // ?>
<figure class="skill-block" style="<?php echo $overlay ?>">
<img class="back-image" src="<?php echo $backImage['url'] ?>"/>
<figcaption>
<div>
<img src="<?php echo $logo['url']?>">
<h4><?php echo $title ?></h4>
</div>
</figcaption>
<a href="<?php echo $url;?>">View more</a>
</figure>
<?php endwhile; //end of while statement
endif;
?>
<h3>Design</h3>
<?php // check if the repeater field has rows of data
if( have_rows('design') ):
// loop through the rows of data
while ( have_rows('design') ) : the_row();
// display a sub field value inside a card
?>
<?php
// VARIABLES //
$title = get_sub_field('title');
$logo = get_sub_field('logo');
$backImage = get_sub_field('background_image');
$overlay = get_sub_field('overlay_gradient');
$url = get_sub_field('url_link');
// THE SKILL BLOCK // ?>
<figure class="skill-block" style="<?php echo $overlay; ?>">
<img class="back-image" src="<?php echo $backImage['url']; ?>"/>
<figcaption>
<div>
<img src="<?php echo $logo['url']; ?>">
<h4> <?php echo $title; ?></h4>
</div>
</figcaption>
<a href="<?php echo $url;?>">View more</a>
</figure>
<?php endwhile; //end of while statement
endif;
?>
<h3>Tools</h3>
<?php // check if the repeater field has rows of data
if( have_rows('tools') ):
// loop through the rows of data
while ( have_rows('tools') ) : the_row();
// display a sub field value inside a card
?>
<?php
// VARIABLES //
$title = get_sub_field('title');
$logo = get_sub_field('logo');
$backImage = get_sub_field('background_image');
$overlay = get_sub_field('overlay_gradient');
$url = get_sub_field('url_link');
// THE SKILL BLOCK // ?>
<figure class="skill-block" style="<?php echo $overlay ?>">
<img class="back-image" src="<?php echo $backImage['url']; ?>"/>
<p>django <?php echo $backImage; ?></p>
<figcaption>
<div>
<img src="<?php echo $logo['url']; ?>">
<h4><?php echo $title; ?></h4>
</div>
</figcaption>
<a href="<?php echo $url;?>">View more</a>
</figure>
<?php endwhile; //end of while statement
endif;
?>
与其将这个东西写出 3 次,然后当我编辑必须进行 3 次更改的结构时,是否可以将此循环结构保存到某种函数中,然后将填充唯一更改部分的参数传递给它(指定顶级字段名称的部分,例如“开发”或“设计”或“工具”。有什么想法吗?我是编程和 PHP espc 的新手,但我正在尝试将 DRY 写作技巧整合到我的习惯。
所以我试了一下,确实有效!我无法直接在互联网上找到答案,因为它与 PHP 相关,这就是我在这里问的原因,但这是我尝试使用我从 javascript 类中记住的内容。
function skillz($section) {
if( have_rows($section) ):
// loop through the rows of data
while ( have_rows($section) ) : the_row();
// display a sub field value inside a card
?>
<?php
// VARIABLES //
$title = get_sub_field('title');
$logo = get_sub_field('logo');
$backImage = get_sub_field('background_image');
$overlay = get_sub_field('overlay_gradient');
$url = get_sub_field('url_link');
// THE SKILL BLOCK // ?>
<figure class="skill-block" style="<?php echo $overlay ?>">
<img class="back-image" src="<?php echo $backImage['url'] ?>"/>
<figcaption>
<div>
<img src="<?php echo $logo['url']?>">
<h4><?php echo $title ?></h4>
</div>
</figcaption>
<a href="<?php echo $url;?>">View more</a>
</figure>
<?php endwhile; //end of while statement
endif;
};
<h2>Skills:</h2>
<h3>Development:</h3>
<?php skillz(development); ?>
<h3>Design</h3>
<?php skillz(design); ?>
<h3>Tools</h3>
<?php skillz(tools); ?>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句