从 ACF 灵活内容中获取最后一行并将其显示在另一页上
get last row from an ACF flexible content and display it on another page
我制作了两个页面,一个首页和一个"basic content page"。
在这个"basic content page"上,我用不同的文字和图片制作了一个灵活的内容。
我正在寻找一种在首页显示 last 行的方法,是否可行?
更新:这是最后一段代码,感谢@Nick Surmanidze,它可以使用 "post object field"(名为 "relation")从另一个页面获取内容。只剩下如何抓取最后一行的问题了
<?php
$post_object = get_field('relation');
if( $post_object ):
// override $post
$post = $post_object;
setup_postdata( $post );
?>
<div>
<?php
// check if the flexible content field has rows of data
if( have_rows('selection') ):
// loop through the rows of data
while ( have_rows('selection') ) :
the_row();
if( get_row_layout() == 'selectionselection' ):
?>
<div class="titre-soustitre">
<div class="menu-content" data-id="id-<?php the_sub_field('id'); ?>">
<p class="demo bis"><span class="sub"> </span></p>
<a href="#" class="expander"><h1><p class="demo title"><?php the_sub_field('title'); ?></p></h1></a>
<p class="demo bis"><span class="sub"><?php the_sub_field('subhead'); ?></span></p>
</div>
</div>
<?php
endif;
endwhile; else :
// no layouts found
endif;
?>
</div>
<?php wp_reset_postdata();// IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
更新 2:为了帮助您理解:这是我通过 $post_object
抓取的另一页的行
<?php
// check if the flexible content field has rows of data
if( have_rows('selection') ):
// loop through the rows of data
while ( have_rows('selection') ) : the_row();
if( get_row_layout() == 'selectionselection' ):?>
<div class="titre-soustitre">
<div class="menu-content" data-id="id-<?php the_sub_field('id');?>">
<p class="demo bis"><span class="sub"> </span></p>
<a href="#" class="expander"><h1><p class="demo title"><?php the_sub_field('title');?></p></h1></a>
<p class="demo bis"><span class="sub"><?php the_sub_field('subhead');?></span></p>
</div>
</div>
<?php endif;
endwhile;
else :
// no layouts found
endif;
?>
我认为您需要向主页添加自定义字段。它可以是一个 "post / page" 字段(不记得它是如何被调用的)。这个想法是在主页后端指示您将从哪个页面 ID 获取最后一行转发器或灵活内容字段。
添加自定义字段以指示主页上的页面 ID。
现在在主页模板上你需要这样写:
$otherPageId = get_field('your_other_page_id');
然后您可以 运行 与代码中的相同,但进入
have_rows('selection')
函数添加第二个参数
have_rows('selection', $otherPageId)
为了指明您要在哪个页面上搜索此字段。
- 要获取最后一行,您可以使用多种方法。一种是将行内容分配给数组,然后使用数组的最后一项,或者这里是另一个片段,可以让您了解如何以 ACF 方式执行此操作:
$repeater = get_field('repeater');
$last_row = end($repeater);
回声$last_row['sub_field'];
我制作了两个页面,一个首页和一个"basic content page"。
在这个"basic content page"上,我用不同的文字和图片制作了一个灵活的内容。
我正在寻找一种在首页显示 last 行的方法,是否可行?
更新:这是最后一段代码,感谢@Nick Surmanidze,它可以使用 "post object field"(名为 "relation")从另一个页面获取内容。只剩下如何抓取最后一行的问题了
<?php
$post_object = get_field('relation');
if( $post_object ):
// override $post
$post = $post_object;
setup_postdata( $post );
?>
<div>
<?php
// check if the flexible content field has rows of data
if( have_rows('selection') ):
// loop through the rows of data
while ( have_rows('selection') ) :
the_row();
if( get_row_layout() == 'selectionselection' ):
?>
<div class="titre-soustitre">
<div class="menu-content" data-id="id-<?php the_sub_field('id'); ?>">
<p class="demo bis"><span class="sub"> </span></p>
<a href="#" class="expander"><h1><p class="demo title"><?php the_sub_field('title'); ?></p></h1></a>
<p class="demo bis"><span class="sub"><?php the_sub_field('subhead'); ?></span></p>
</div>
</div>
<?php
endif;
endwhile; else :
// no layouts found
endif;
?>
</div>
<?php wp_reset_postdata();// IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
更新 2:为了帮助您理解:这是我通过 $post_object
抓取的另一页的行 <?php
// check if the flexible content field has rows of data
if( have_rows('selection') ):
// loop through the rows of data
while ( have_rows('selection') ) : the_row();
if( get_row_layout() == 'selectionselection' ):?>
<div class="titre-soustitre">
<div class="menu-content" data-id="id-<?php the_sub_field('id');?>">
<p class="demo bis"><span class="sub"> </span></p>
<a href="#" class="expander"><h1><p class="demo title"><?php the_sub_field('title');?></p></h1></a>
<p class="demo bis"><span class="sub"><?php the_sub_field('subhead');?></span></p>
</div>
</div>
<?php endif;
endwhile;
else :
// no layouts found
endif;
?>
我认为您需要向主页添加自定义字段。它可以是一个 "post / page" 字段(不记得它是如何被调用的)。这个想法是在主页后端指示您将从哪个页面 ID 获取最后一行转发器或灵活内容字段。
添加自定义字段以指示主页上的页面 ID。
现在在主页模板上你需要这样写: $otherPageId = get_field('your_other_page_id');
然后您可以 运行 与代码中的相同,但进入
have_rows('selection')
函数添加第二个参数
have_rows('selection', $otherPageId)
为了指明您要在哪个页面上搜索此字段。
- 要获取最后一行,您可以使用多种方法。一种是将行内容分配给数组,然后使用数组的最后一项,或者这里是另一个片段,可以让您了解如何以 ACF 方式执行此操作:
$repeater = get_field('repeater');
$last_row = end($repeater);
回声$last_row['sub_field'];