has_sub_field()

上次更新日期:2024年4月4日

描述

此函数已弃用。请使用have_rows()而不是函数。

此函数在while-lop中用于循环遍历中继器或灵活内容字段的每一行。

不同于拥有(_rows),此函数每次调用时都会在可用行中执行一步,当也在if-statement中使用时,会导致意外的结果。

参数

has_sub_field($field_name,$post_id);
  • $字段名 (字符串) (必需)要检索的Repeater或Flexible Content字段的名称。例如“gallery_images”
  • $帖子id (混合) (可选)保存值的帖子ID。默认为当前帖子。

返回

(布尔)如果存在行,则为True。

更改日志

  • 版本4.3.0中已弃用

示例

循环通过中继器字段

此示例演示如何循环通过名为“gallery_images”的转发器字段。

<?php if(get_field('gallery_images')):?><?php while(has_sub_field('gallery_images')):?><img src=“<?php the_sub_field('image');?>”alt=“>?php the _sub_feld('alt');”/><?php endwhile;?><?php-endif;

循环浏览“灵活内容”字段

此示例演示了如何循环访问名为“内容”的灵活内容字段并显示各种布局。

<?php while(has_sub_field(“内容”)):?><?php if(get_row_layout()==“段落”):?><div><?php the_sub_field(“内容”);?></div><?php elseif(get_row_layout()==“文件”):?><div><a href=“<?php the_sub_field(”file“);?>”><?php the_sub_field(“名称”);?></a></div><?php elseif(get_row_layout()==“featured_posts”):?><div><h2><?php the_sub_field(“标题”);?></氢气><?php the_sub_field(“内容”);?><?php-if(get_sub_field(“posts”)):?><ul><?php foreach(get_sub_field(“posts”)作为$p):?><li><a href=“<?php echo get_permalink($p->ID);?>”><?php echo get_the_title($p->ID);?></a></li><?php endforeach;?></ul><?php-endif;?></div><?php-endif;?><?php endwhile;?>

循环访问嵌套的Repeater字段

此示例演示如何循环嵌套的Repeater字段并显示子字段数据。

<?php if(get_field(“父转发器”):?><?php while(has_sub_field('父转发器')):?><div><?php if(get_sub_field('child_reporter')):?><ul><?php while(has_sub_field('child_reporter')):?><li><?php子字段(“项目”);?></li><?php endwhile;?></ul><?php-endif;?></div><?php endwhile;?><?php-endif;

循环访问嵌套的Repeater字段(来自另一个$post ID)

这个例子演示了如何循环使用ID为123的不同帖子中的嵌套Repeater字段。

注:您无需指定$帖子id用于任何子字段函数。

<?php if(get_field(“父转发器”,123)):?><?php while(has_sub_field('parent_reporter',123)):?><div><?php(电话)if(获取子字段(“儿童中继器”):?><ul><?php while(has_sub_field('child_reporter')):?><li><?php子字段(“项目”);?></li><?php endwhile;?></ul><?php-endif;?></div><?php endwhile;?><?php-endif;

相关的