高级自定义字段显示最后三个子中继器行
Advanced Custom Fields display last three sub-repeater rows
我正在使用高级自定义字段 (ACF) 从事件页面中提取转发器信息并在主页上显示缩短的事件列表。
我已经设置了一个中继器以允许用户输入事件将在哪个月份发生(允许他们输入多个月的事件),然后是一个子中继器以允许他们为以下事件添加多个事件给定的月份。示例如下:
三月
- 3 月 9 日活动
- 3 月 12 日活动
- 3 月 28 日活动
4 月
- 4 月 1 日活动
- 4 月 28 日活动
这是事件页面上的当前输出,并且按预期工作。
在网站首页,我需要拉取3个最新的(列表底部的事件为最新的事件)显示在首页。
我在主页上拉取和显示事件没有问题。我遇到的问题是当最后三个事件(子转发器)跨月(父转发器)时显示事件。
在 if、while 语句中使用 php 循环简单地限制事件输出仅限制该月输出的事件数。我目前在主页上使用的代码如下。
<?php if( have_rows('event_month', 1263)): ?>
<ul>
<?php while ( have_rows('event_month', 1263) ) : the_row(); ?>
<?php if( have_rows('event', 1263)):; ?>
<?php while ( have_rows('event', 1263) ) : the_row(); ?>
<li>
<h3>
<a href="<?php echo esc_url( home_url( '/' ) ); ?>events/"><?php $summary = get_sub_field('event_title');
echo substr($summary, 0, 34),'...'; ?></a>
<span><?php the_sub_field('event_day_of_week');?>, <?php the_sub_field('event_sub_month');?> <?php the_sub_field('event_day');?></span>
</h3>
</li>
<?php endwhile; ?>
<?php else: ?>
<p>Show dates to be announced soon.</p><?php the_sub_field('event_title'); ?>
<?php endif; ?>
<?php endwhile; ?>
</ul>
如果我们捕获三个最新事件,我希望主页上的输出会是什么样子:
- 3 月 28 日活动
- 4 月 1 日活动
- 4 月 28 日活动
您可能应该使用 for
而不是 while
。并考虑以下算法:
1) Get the last row from event_month
2) Count the number of events in that month
3) If the number of events are more than or equal to 3.
3.1) Get last 3 events and display them
4) Else count the number of remaing events (3-<<events in last month>>
)
4.1) Now get the second last row and repeat steps 2,3,4
所以使用上面的逻辑你的代码应该是这样的:
<?php
function getEvents($rows, $noOfEvents){
$resultArray = array();
if($rows && count($rows > 0)) {
$events = $rows[count($rows)-1]['event'];
$events = is_array($events) ? $events : array();
$eventCount = count($events);
if($eventCount < $noOfEvents){
$noOfOtherEvents = $noOfEvents-$eventCount;
array_pop($rows);
$iterate = getEvents($rows,$noOfOtherEvents);
$resultArray = array_merge($events,$iterate);
}
else{
$resultArray = array_slice($rows, 0-$eventCount, $eventCount);
}
return $resultArray;
}
$rows = get_field('event_month', 1263);
if($rows) {
$requiredEvents = getEvents($rows,3); //3 or how many ever last you want
foreach($requiredEvents as $event){
var_dump($event); //this should have all you need like $event['event_title'],$event['event_day'],ect...
}
}
这可能不是每个人都在寻找的答案,但这是我作为变通方法所做的,对我来说效果很好。
我最终解决了 php 之外的问题,使用 css 到 select 最后三个列表项。这是我用过的,效果很好。
.connect-list-wrapper ul li
{
display: none;
}
.connect-list-wrapper ul li:nth-last-child(-n+3)
{
display: block;
}
我正在使用高级自定义字段 (ACF) 从事件页面中提取转发器信息并在主页上显示缩短的事件列表。
我已经设置了一个中继器以允许用户输入事件将在哪个月份发生(允许他们输入多个月的事件),然后是一个子中继器以允许他们为以下事件添加多个事件给定的月份。示例如下:
三月
- 3 月 9 日活动
- 3 月 12 日活动
- 3 月 28 日活动
4 月
- 4 月 1 日活动
- 4 月 28 日活动
这是事件页面上的当前输出,并且按预期工作。
在网站首页,我需要拉取3个最新的(列表底部的事件为最新的事件)显示在首页。
我在主页上拉取和显示事件没有问题。我遇到的问题是当最后三个事件(子转发器)跨月(父转发器)时显示事件。
在 if、while 语句中使用 php 循环简单地限制事件输出仅限制该月输出的事件数。我目前在主页上使用的代码如下。
<?php if( have_rows('event_month', 1263)): ?>
<ul>
<?php while ( have_rows('event_month', 1263) ) : the_row(); ?>
<?php if( have_rows('event', 1263)):; ?>
<?php while ( have_rows('event', 1263) ) : the_row(); ?>
<li>
<h3>
<a href="<?php echo esc_url( home_url( '/' ) ); ?>events/"><?php $summary = get_sub_field('event_title');
echo substr($summary, 0, 34),'...'; ?></a>
<span><?php the_sub_field('event_day_of_week');?>, <?php the_sub_field('event_sub_month');?> <?php the_sub_field('event_day');?></span>
</h3>
</li>
<?php endwhile; ?>
<?php else: ?>
<p>Show dates to be announced soon.</p><?php the_sub_field('event_title'); ?>
<?php endif; ?>
<?php endwhile; ?>
</ul>
如果我们捕获三个最新事件,我希望主页上的输出会是什么样子:
- 3 月 28 日活动
- 4 月 1 日活动
- 4 月 28 日活动
您可能应该使用 for
而不是 while
。并考虑以下算法:
1) Get the
last row from event_month
2) Count the number of events in that month
3) If the number of events are more than or equal to 3.
3.1) Get last 3 events and display them
4) Else count the number of remaing events (
3-<<events in last month>>
)4.1) Now get the second last row and repeat steps 2,3,4
所以使用上面的逻辑你的代码应该是这样的:
<?php
function getEvents($rows, $noOfEvents){
$resultArray = array();
if($rows && count($rows > 0)) {
$events = $rows[count($rows)-1]['event'];
$events = is_array($events) ? $events : array();
$eventCount = count($events);
if($eventCount < $noOfEvents){
$noOfOtherEvents = $noOfEvents-$eventCount;
array_pop($rows);
$iterate = getEvents($rows,$noOfOtherEvents);
$resultArray = array_merge($events,$iterate);
}
else{
$resultArray = array_slice($rows, 0-$eventCount, $eventCount);
}
return $resultArray;
}
$rows = get_field('event_month', 1263);
if($rows) {
$requiredEvents = getEvents($rows,3); //3 or how many ever last you want
foreach($requiredEvents as $event){
var_dump($event); //this should have all you need like $event['event_title'],$event['event_day'],ect...
}
}
这可能不是每个人都在寻找的答案,但这是我作为变通方法所做的,对我来说效果很好。
我最终解决了 php 之外的问题,使用 css 到 select 最后三个列表项。这是我用过的,效果很好。
.connect-list-wrapper ul li
{
display: none;
}
.connect-list-wrapper ul li:nth-last-child(-n+3)
{
display: block;
}