是否有任何方式或方法来连接while循环内的HTML字符串?



是否有办法在字符串内连接while循环?基本上,我需要一个HTML字符串,有一些元数据,我想从自定义的帖子类型检索,并希望将该数据存储到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>';
}

如果你需要返回html代码,我们可以先形成代码,把它放入一个变量中,然后返回该变量。

试试这个代码

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;
}

相关内容

  • 没有找到相关文章

最新更新