programing

[ Advanced Custom Fields ](상세 커스텀필드)에는 마지막 3개의 하위 반복행 표시

lovejava 2023. 3. 20. 21:12

[ Advanced Custom Fields ](상세 커스텀필드)에는 마지막 3개의 하위 반복행 표시

Advanced Custom Fields(ACF; 상세 커스텀필드)를 사용하여 이벤트페이지에서 리피터 정보를 꺼내 홈 페이지에 이벤트의 단축 리스트를 표시하고 있습니다.

사용자가 이벤트를 몇 월에 실행할지 입력할 수 있도록 리피터를 설정한 후(여러 달 이벤트에 추가할 수 있도록 허용), 서브 리피터를 설정하여 해당 달에 여러 이벤트를 추가할 수 있도록 했습니다.다음 예:

3월

  • 3월 9일 행사
  • 3월 12일 행사
  • 3월 28일 행사

4월

  • 4월 1일 행사
  • 4월 28일 행사

이것은 이벤트 페이지의 현재 출력으로, 의도대로 동작합니다.

홈페이지에서 최신 이벤트 3개(목록 하단에 있는 이벤트가 최신 이벤트)를 가져와 홈페이지에 표시해야 합니다.

홈 페이지에 이벤트를 끌어다 표시하는 데 문제가 없습니다.문제가 있는 것은 마지막 3개의 이벤트(자녀 리피터)가 월(부모 리피터) 사이에 교차했을 때의 이벤트를 표시하는 것입니다.

php loop을 사용하여 이벤트 출력을 제한하는 것만으로 해당 달의 이벤트 출력 수를 제한할 수 있습니다.현재 홈페이지에서 사용하고 있는 코드는 다음과 같습니다.

<?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) 고객님의last row from event_month

2) 해당 달의 이벤트 수를 카운트합니다.

3) 이벤트 수가 3 이상일 경우

3.1) 최근 3개의 이벤트를 취득하여 표시한다.

4) 그렇지 않으면 리마잉 이벤트 수를 카운트합니다(3-<<events in last month>>)

4.1) 두 번째 마지막 행을 가져와 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를 사용하여 마지막 3개의 목록 항목을 선택했습니다.여기 내가 사용한 게 있어, 잘 작동했어.

.connect-list-wrapper ul li
{
    display: none;
}
.connect-list-wrapper ul li:nth-last-child(-n+3) 
{
    display: block;
}

언급URL : https://stackoverflow.com/questions/43106718/advanced-custom-fields-display-last-three-sub-repeater-rows