php - 返回值时Wordpress ACF转发器不循环
问题描述
出于某种原因,我的循环将只显示转发器的第一行。如何让循环为已添加的所有行创建链接?
function related_pages_shortcode2() {
if( have_rows('related_pages') ):
while( have_rows('related_pages') ): the_row();
$type = get_sub_field('type');
$name = get_sub_field('name');
$link = get_sub_field('url');
$related_page = '<strong>' . $type . ': </strong>' . '<a href="' . $link . '">' . $name . '</a>';
return $related_page;
endwhile;
else :
endif;
}
add_shortcode( 'related_pages2', 'related_pages_shortcode2' );
解决方案
您正在返回函数(阻止函数进一步执行),而不是保存先前的输出并将新行附加到它。像这样更改您的函数,以便它还保存在 while 循环中生成的先前内容:
if( have_rows('related_pages') ):
$output = ''; // initialize the output buffer as an empty string
while( have_rows('related_pages') ): the_row();
$type = get_sub_field('type');
$name = get_sub_field('name');
$link = get_sub_field('url');
$related_page = '<strong>' . $type . ': </strong>' . '<a href="' . $link . '">' . $name . '</a>';
$output .= $related_page; // in this way you append the new row to the previous output
endwhile;
endif;
推荐阅读
- powercli - 从脚本中排除虚拟机列表删除 vmware 中的快照
- python - 尝试在 python 中导入 pouchdb 时出错
- dynamic - 国王的进行曲
- html - 如何将 Bootstrap 多个轮播幻灯片横幅停止为单个图像横幅?
- php - PHP如何删除本地机器上的文件
- javascript - Firebase 云函数:await admin.dbRef.once('value')
- javascript - JavaScript 代码的词法环境是否包含 ThisBinding 或执行上下文是否包含它?
- python - 使用谓词过滤 pyarrow.parquet.ParquetDataset 中的行
- npm - package.json 文件中的符号 @^ 是什么意思
- html - 如何将一个 div 平均居中到另一个 div 中?