有什么方法或方法可以在 HTML 的字符串内连接 while 循环吗?

Is there any way or method to concatenate while loop inside the string of HTML?

有什么方法可以在字符串中连接 while 循环吗?基本上,我需要一个 HTML 的字符串,并且我想从自定义 post 类型中检索一些元数据,并希望将该数据存储到 HTML 中。 预先感谢您的帮助。

function staffSectionOutput($props) {
    $args = array(  
        'post_type' => 'staff_section',
        'category_name'       =>  'senior'
    );
    
    $loop = new WP_Query( $args ); 
  
    

  return '<section  class="staff">
  
            <div class="container">
                <div class="row">
                   '. while ( $loop->have_posts() ) { 
                    $loop->the_post(); 
                    $employeeName = get_post_meta( get_the_ID() , 'staff-employee-name', false );
                    $employeJob = get_post_meta( get_the_ID() , 'staff-employee-job-title', false );
                    $employeDescription = get_post_meta( get_the_ID() , 'staff-employee-description', false );
                    $employeUrl = get_post_meta( get_the_ID() , 'staff-employee-url', false ); .'
                    <div class="col-12 col-sm-6 col-md-4">
                        <div data-description="'. $employeUrl .'">
                            <div class="card-head">
                                <img src="'. $employeUrl .'">
                            </div>
                            <div class="card-body">
                                <div class="text">
                                    <h4>'. $employeeName .'</h4>
                                    <span>'. $employeJob .'</span>
                                </div>
                                <div class="btn-a">Learn More</div>
                            </div>
                        </div>
                    </div> 
                    '.
                }
                  
                wp_reset_postdata(); .'
                </div>
            </div>
    </section>';
}

如果您需要return html代码,我们可以先形成该代码,将其放入一个变量,然后return该变量。

试试这个代码

function staffSectionOutput($props) {
    $args = array(  
        'post_type' => 'staff_section',
        'category_name'       =>  'senior'
    );
    
    $loop = new WP_Query( $args ); 
// enable output buffer
ob_start();
?>
  <section  class="staff">
  
            <div class="container">
                <div class="row">
                <?php    while ( $loop->have_posts() ) { 
                    $loop->the_post(); 
                    $employeeName = get_post_meta( get_the_ID() , 'staff-employee-name', false );
                    $employeJob = get_post_meta( get_the_ID() , 'staff-employee-job-title', false );
                    $employeDescription = get_post_meta( get_the_ID() , 'staff-employee-description', false );
                    $employeUrl = get_post_meta( get_the_ID() , 'staff-employee-url', false );
                    ?>
                    <div class="col-12 col-sm-6 col-md-4">
                        <div data-description="<?php echo $employeUrl ?>">
                            <div class="card-head">
                                <img src="<?php echo $employeUrl ?>">
                            </div>
                            <div class="card-body">
                                <div class="text">
                                    <h4><?php echo $employeeName ?></h4>
                                    <span><?php echo $employeJob ?></span>
                                </div>
                                <div class="btn-a">Learn More</div>
                            </div>
                        </div>
                    </div> 

               <?php } ?>
                  
               <?php wp_reset_postdata(); ?>
                </div>
            </div>
    </section>
<?php
// save everything in the buffer to the variable $content
$content = ob_get_contents();
 
// disable and clear the buffer
ob_end_clean();
return $content;
}