Php ACF-Repeater仅显示最后输入的行,而不是post中的所有行
我在functions.php文件中有以下代码:Php ACF-Repeater仅显示最后输入的行,而不是post中的所有行,php,wordpress,custom-fields,advanced-custom-fields,Php,Wordpress,Custom Fields,Advanced Custom Fields,我在functions.php文件中有以下代码: while ( have_rows('origin-repiter') ): the_row(); $origin = get_sub_field('origin-rep'); endwhile; while ( have_rows('destination-repiter') ): the_row(); $destination = get_sub_field('destination-rep'); endwhile; 以下是源站rep
while ( have_rows('origin-repiter') ): the_row();
$origin = get_sub_field('origin-rep');
endwhile;
while ( have_rows('destination-repiter') ): the_row();
$destination = get_sub_field('destination-rep');
endwhile;
以下是源站repiter/目标站repiter中继器字段名称,而源站rep/目标站rep是中继器字段,如果需要,可以是行
我的问题是,在我的WP站点的前端,ACF只显示来自后端的中继器字段的最后输入
例如:
对于我输入的来源:
伦敦、巴黎、莫斯科(三排)目的地相同,但目的地相反(莫斯科、巴黎、伦敦)
但在前端ACF上只显示最后一个条目
出发地:莫斯科,目的地:伦敦
这是functions.php中的一段代码,它在前端呈现这两个字段/代码段的php/HTML:
$output .= '<div class="post_offer"><div class="offer-header"><div class="offer-pr"><div class="offer-pr-t"> from</div> <div class="offer-pr-v"><div class=""><div class="offer-pr-c">'.$fcurrency_icon .'</div><span class="price" style="font-size:30px;">'.$fprice.'</span></div></div></div> <h2 class="offer-tl editor-paragraph-tags">'.$ftitle.'</h2></div><div class="offer-rt-rd"><div class="offer-rt-rd-o"><div class="offer-rt-rd-lb">Origin</div><div class="offer-rt-rd-lo">'.$origin.'</div></div><div class="offer-rt-rd-d"><div class="offer-rt-rd-lb">Destination</div><div class="offer-rt-rd-lo">'.$destination .'</div></div><div class="offer-rt-rd-ico"></div></div>';
$output.='from'.$fcurrency_icon.'.$fprice.'.$ftitle.'Origin'.$Origin.'Destination'.$Destination'.';
感谢任何帮助
附录-全部代码:
function add_box_post_end($content){
global $post;
$postId = $post->ID;
$title = get_field('title_text', $postId);
$ftitle = ($title != '')? $title :'';
$currency_icon = get_field('currency_icon', $postId);
$fcurrency_icon = ($currency_icon != '')? $currency_icon :'£';
$price = get_field('price', $postId);
$fprice = ($price != '')? $price :'99';
if( have_rows('origin-destination') ): /* your_repeater_name corresponds to the name of your repeater field */
// loop through the rows of data
while ( have_rows('origin-destination') ) : the_row();
// or save the sub fields in a variable
$origin = get_sub_field('origin');
$destination= get_sub_field('destination');
endwhile;
else :
// no rows found
endif;
$zbor_text = (get_field('zbor_text')!='')?get_field('zbor_text'):'';
$rr = get_field('zbor_images');
if(get_field('zbor_images')!=''){
$zbor_imagess = get_field('zbor_images');
$zbor_images = '';
foreach($zbor_imagess as $zbor_image){
$imgUrl = $zbor_image['url'];
$imgAlt = $zbor_image['title'];
$imgLink = ($zbor_image['alt']!='')?$zbor_image['alt']:'javascript:;';
$zbor_images .= '<a href="'. $imgLink .'" target="_blank" style="display:block;"><img src="' . $imgUrl . '" alt="'. $imgAlt .'" /></a><br />';
}
}else{
$zbor_images = '';
}
$cazare_text = (get_field('cazare_text')!='')?get_field('cazare_text'):'';
if(get_field('cazare_images')!=''){
$cazare_imagess = get_field('cazare_images');
$cazare_images = '';
foreach($cazare_imagess as $cazare_image){
$imgUrl = $cazare_image['url'];
$imgAlt = $cazare_image['title'];
$imgLink = ($cazare_image['alt']!='')?$cazare_image['alt']:'javascript:;';
$cazare_images .= '<a href="'. $imgLink .'" target="_blank" style="display:block;"><img src="' . $imgUrl . '" alt="'. $imgAlt .'" /></a><br />';
}
}else{
$cazare_images = '';
}
$inchiriere_masina_text = (get_field('inchiriere_masina_text')!='')?get_field('inchiriere_masina_text'):'';
if(get_field('inchiriere_masina_images')!=''){
$inchiriere_masina_imagess = get_field('inchiriere_masina_images');
$inchiriere_masina_images = '';
foreach($inchiriere_masina_imagess as $inchiriere_masina_image){
$imgUrl = $inchiriere_masina_image['url'];
$imgAlt = $inchiriere_masina_image['title'];
$imgLink = ($inchiriere_masina_image['alt']!='')?$inchiriere_masina_image['alt']:'javascript:;';
$inchiriere_masina_images .= '<a href="'. $imgLink .'" target="_blank" style="display:block;"><img src="' . $imgUrl . '" alt="'. $imgAlt .'" /></a><br />';
}
}else{
$inchiriere_masina_images = '';
}
$transfer_aeroport_text = (get_field('transfer_aeroport_text')!='')?get_field('transfer_aeroport_text'):'';
if(get_field('transfer_aeroport_images')!=''){
$transfer_aeroport_imagess = get_field('transfer_aeroport_images');
$transfer_aeroport_images = '';
foreach($transfer_aeroport_imagess as $transfer_aeroport_image){
$imgUrl = $transfer_aeroport_image['url'];
$imgAlt = $transfer_aeroport_image['title'];
$imgLink = ($transfer_aeroport_image['alt']!='')?$transfer_aeroport_image['alt']:'javascript:;';
$transfer_aeroport_images .= '<a href="'. $imgLink .'" target="_blank" style="display:block;"><img src="' . $imgUrl . '" alt="'. $imgAlt .'" /></a><br />';
}
}else{
$transfer_aeroport_images = '';
}
$escala_text = (get_field('escala_text')!='')?get_field('escala_text'):'';
if(get_field('escala_images')!=''){
$escala_imagess = get_field('escala_images');
$escala_images = '';
foreach($escala_imagess as $escala_image){
$imgUrl = $escala_image['url'];
$imgAlt = $escala_image['title'];
$imgLink = ($escala_image['alt']!='')?$escala_image['alt']:'javascript:;';
$escala_images .= '<a href="'. $imgLink .'" target="_blank" style="display:block;"><img src="' . $imgUrl . '" alt="'. $imgAlt .'" /></a><br />';
}
}else{
$escala_images = '';
}
$button_one_text = (get_field('button_one_text')!='')?get_field('button_one_text'):'CLICK HERE TO BOOK';
$button_one_url = (get_field('button_one_url')!='')?get_field('button_one_url'):'#';
$buttonOne = '<a class="offer-book-lnk" href="'.$button_one_url.'" target="_blank" rel="nofollow">'. $button_one_text .'</a>';
$button_two_text = (get_field('button_two_text')!='')?get_field('button_two_text'):'HOTEL COMPARISON TOOL';
$button_two_url = (get_field('button_two_url')!='')?get_field('button_two_url'):'#';
$buttonTwo = '<a class="offer-book-lnk" href="'. $button_two_url .'" target="_blank" rel="nofollow">'. $button_two_text .'</a>';
$zbor = ($zbor_text!='' || $zbor_images!='')? '[tabby title="ZBOR"]'. $zbor_text . '<br />' . $zbor_images:'';
$cazare = ($cazare_text!='' || $cazare_images!='')? '[tabby title="CAZARE"]'. $cazare_text . '<br />' . $cazare_images:'';
$inchiriere_masina = ($inchiriere_masina_text!='' || $inchiriere_masina_images!='')? '[tabby title="INCHIRIERE MASINA"]'. $inchiriere_masina_text . '<br />' . $inchiriere_masina_images:'';
$transfer_aeroport = ($transfer_aeroport_text!='' || $transfer_aeroport_images!='')? '[tabby title="TRANSFER AEROPORT"]'. $transfer_aeroport_text . '<br />' . $transfer_aeroport_images:'';
$escala = ($escala_text!='' || $escala_images!='')? '[tabby title="ESCALA"]'. $escala_text . '<br />' . $escala_images:'';
$output = '';
if(is_single() && $ftitle!='' && $price!=''){
$output .= '<div class="post_offer"><div class="offer-header"><div class="offer-pr"><div class="offer-pr-t"> from</div> <div class="offer-pr-v"><div class=""><div class="offer-pr-c">'.$fcurrency_icon .'</div><span class="price" style="font-size:30px;">'.$fprice.'</span></div></div></div> <h2 class="offer-tl editor-paragraph-tags">'.$ftitle.'</h2></div><div class="offer-rt-rd"><div class="offer-rt-rd-o"><div class="offer-rt-rd-lb">Origin</div><div class="offer-rt-rd-lo">'.$origin.'</div></div><div class="offer-rt-rd-d"><div class="offer-rt-rd-lb">Destination</div><div class="offer-rt-rd-lo">'.$destination .'</br></div></div><div class="offer-rt-rd-ico"></div></div>';
$output .= do_shortcode( $zbor . $cazare . $inchiriere_masina . $transfer_aeroport . $escala . '[tabbyending]');
$output .= '<div class="offer_footer"><!--<ul class="offer-book">
<li class="offer-book-it">' . $buttonOne . '</li>
<li class="offer-book-it">' . $buttonTwo . '</li>
</ul>--></div></div>';
}
$content .= $output;
return $content;
function add\u box\u post\u end($content){
全球$员额;
$postId=$post->ID;
$title=get_字段($title_text',$posted);
$ftitle=($title!='')$title:'';
$currency\u icon=get\u字段($currency\u icon',$posted);
$fcurrency_icon=($currency_icon!='')?$currency_icon:';
$price=get_字段($price',$posted);
$fprice=($price!='')?$price:'99';
if(have_rows('origin-destination'):/*您的_repeater_名称对应于您的repeater字段的名称*/
//循环遍历数据行
while(have_rows('origin-destination')):the_row();
//或者将子字段保存在变量中
$origin=获取子字段(“origin”);
$destination=get_sub_字段(“destination”);
结束时;
其他:
//找不到行
endif;
$zbor_text=(get_字段('zbor_text')!=“”)?get_字段('zbor_text'):“”;
$rr=get_字段(“zbor_图像”);
如果(获取_字段('zbor_图像')!=“”){
$zbor_imagess=get_字段('zbor_images');
$zbor_图像=“”;
foreach($zbor_image作为$zbor_image){
$imgUrl=$zbor_图像['url'];
$imgAlt=$zbor_图像['title'];
$imgLink=($zbor_image['alt']!='')$zbor_image['alt']:'javascript:';
$zbor_图像。='
';
}
}否则{
$zbor_图像=“”;
}
$cazare_text=(获取字段('cazare_text')!=“”)?获取字段('cazare_text'):“”;
如果(获取字段('cazare_图像')!=“”){
$cazare_imagess=get_字段('cazare_images');
$cazare_图像=“”;
foreach($cazare_图像作为$cazare_图像){
$imgUrl=$cazare_图像['url'];
$imgAlt=$cazare_图像['title'];
$imgLink=($cazare_image['alt']!='')?$cazare_image['alt']:'javascript:';
$cazare_图像=“
”;
}
}否则{
$cazare_图像=“”;
}
$inchiriere_masina_text=(获取字段('inchiriere_masina_text')!=“”)?获取字段('inchiriere_masina_text'):“”;
如果(获取图像字段('inchiriere\u masina\u images')!=“”){
$inchiriere_masina_imagess=get_字段('inchiriere_masina_images');
$inchiriere_masina_images='';
foreach($inchiriere\u masina\u图像作为$inchiriere\u masina\u图像){
$imgUrl=$inchiriere_masina_image['url'];
$imgAlt=$inchiriere_masina_图像['title'];
$imgLink=($inchiriere_masina_image['alt']!='')-$inchiriere_masina_image['alt']:'javascript:';
$inchiriere_masina_images.='
';
}
}否则{
$inchiriere_masina_images='';
}
$transfer_aeroport_text=(获取字段('transfer_aeroport_text')!=“”)?获取字段('transfer_aeroport_text'):“”;
如果(获取字段('transfer_aeroport_images')!=“”){
$transfer_aeroport_imagess=获取字段(“transfer_aeroport_images”);
$transfer_aeroport_images='';
foreach($transfer\u aeroport\u图像作为$transfer\u aeroport\u图像){
$imgUrl=$transfer_airport_image['url'];
$imgAlt=$transfer_airport_image['title'];
$imgLink=($transfer\u airport\u image['alt']!='')?$transfer\u airport\u image['alt']:'javascript:';
$transfer_aeroport_图像。='
';
}
}否则{
$transfer_aeroport_images='';
}
$escala_text=(获取字段('escala_text')!=“”)?获取字段('escala_text'):“”;
如果(获取字段('escala_图像')!=“”){
$escala_imagess=get_字段('escala_images');
$escala_图像=“”;
foreach($escala_图像作为$escala_图像){
$imgUrl=$escala_图像['url'];
$imgAlt=$escala_图像['title'];
$imgLink=($escala_image['alt']!='')?$escala_image['alt']:'javascript:';
$escala_图像=“
”;
}
}否则{
$escala_图像=“”;
}
$button_one_text=(获取字段(“button_one_text”)!=“”)?获取字段(“button_one_text”):“单击此处预订”;
$button_one_url=(获取字段('button_one_url')!=“”)?获取字段('button_one_url'):';
$buttone='';
$button_two_text=(获取字段('button_two_text')!=“”)?获取字段('button_two_text'):'HOTEL COMPARISON TOOL';
$button\u two\u url=(获取字段('button\u two\u url')!=“”)?获取字段('button\u two\u url'):';
$buttonTwo='';
$zbor=($zbor|u text!=''| |$zbor_images!='')'[tabby title=“zbor”].$zbor_text.
.$zbor_images:'';
$cazare=($cazare|u text!=“cazare|u images!=”)[tabby title=“cazare”]。$cazare_text.
。$cazare_images:“”;
$inchiriere_masina=($inchiriere_masina_text!=“inchiriere|u masina_images!=”)“[tabby title=“inchiriere masina”]。$inchiriere_masina_text.
”。$inchiriere_masina_images:”;
$transfer_aeroport=($transfer_aeroport_text!=''transfer_aeroport_images!='')[tabby title=“transfer aeroport”]。$transfer_aeroport_text.
。$transfer_aeroport_images:'';
$escala=($escala|u text!=“escala|u images!=”)“[tabby title=“escala”]”.$escala_text.“
.$escala_images:”;
$output='';
如果(是单(&&$ftitle!=''&&$price!=''){
$output.='from'.$fcurrency_icon.'.$fprice.'.$ftitle.'Origin.'Destination.'.$Destination.'.$Destination.'';
$output.=do_shortcode($zbor.$cazare.$inchiriere_masina.$transfer_airport.$escala.“[tabbyending]”);
$output.='';
}
$content.=$output;
返回$content;
您的第一个代码示例没有多大意义,因为您不断地覆盖$origin和$destination的值。
毛皮
<?php
// check if the repeater field has rows of data
if( have_rows('your_repeater_name') ): /* your_repeater_name corresponds to the name of your repeater field */
// loop through the rows of data
while ( have_rows('your_repeater_name') ) : the_row();
// display the sub field (the function the_sub_field echos the values directly)
the_sub_field('origin'); /* sub-field with name "origin" */
the_sub_field('destination'); /* sub-field with name "destination" */
// or save the sub fields in a variable
$origin = get_sub_field('origin');
$destination= get_sub_field('destination');
// and then echo them
// you need to output them **INSIDE** the while loop
echo $origin;
echo $destination;
endwhile;
else :
// no rows found
endif;
?>
function print_origin_dest(){
// check if the repeater field has rows of data
if( have_rows('your_repeater_name') ):
// loop through the rows of data
while ( have_rows('your_repeater_name') ) : the_row();
// get the acf values
$origin = get_sub_field('origin');
$destination= get_sub_field('destination');
// add the title and icon html here (you have not defined them in your question)
$ftitle = 'Your title';
$fcurrency_icon = '';
// render the content
$output = '<div class="post_offer"><div class="offer-header"><div class="offer-pr"><div class="offer-pr-t"> from</div> <div class="offer-pr-v"><div class=""><div class="offer-pr-c">'.$fcurrency_icon .'</div><span class="price" style="font-size:30px;">'.$fprice.'</span></div></div></div> <h2 class="offer-tl editor-paragraph-tags">'.$ftitle.'</h2></div><div class="offer-rt-rd"><div class="offer-rt-rd-o"><div class="offer-rt-rd-lb">Origin</div><div class="offer-rt-rd-lo">'.$origin.'</div></div><div class="offer-rt-rd-d"><div class="offer-rt-rd-lb">Destination</div><div class="offer-rt-rd-lo">'.$destination .'</div></div><div class="offer-rt-rd-ico"></div></div>';
echo $output;
endwhile;
else :
// no rows found
endif;
}
<?php print_origin_dest(); ?>