是否可以将WordPress/PHP查询循环分配给自定义函数以传递参数



我正在使用高级自定义字段在我的投资组合网站上创建交互式简历。在技能部分中,我有三个类别。开发,设计和工具。我在其中的每一个上使用中继器自定义字段通过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次更改的结构时,可以将循环结构保存到某种功能,然后传递填充唯一的IT参数更改的部分(指定"开发"或"设计"或"工具"之类的顶级字段的名称。任何想法?我是编程和PHP ESPC的新想法,但我正在尝试整合干燥的写作习惯的技能。

怎么样?

<?php
    $skills = array( 'development', 'design', 'tools' );
    foreach ($skills as $skill) :
?>
    <h3><?php echo ucfirst( $skill ); ?>:</h3>
    <?php    // check if the repeater field has rows of data
        if( have_rows( $skill ) ):
        // loop through the rows of data
        while ( have_rows( $skill ) ) : 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');
    ?>
        <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;
    endforeach;
?>

,所以我尝试了它,并且确实有效!我无法直接在Internet上找到与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); ?>

最新更新