programing

ACF의 유연한 컨텐츠에서 마지막 행을 가져와 다른 페이지에 표시

bestprogram 2023. 10. 29. 19:54

ACF의 유연한 컨텐츠에서 마지막 행을 가져와 다른 페이지에 표시

저는 1면과 "기본적인 내용 페이지"라는 두 페이지를 만들었습니다.
이 '기본 콘텐츠 페이지'에서는 텍스트와 이미지가 다른 유연한 콘텐츠를 만들었습니다.

맨 앞줄에 마지막 줄을 표시하는 방법을 검색해봤는데, 가능한가요?

업데이트 : 마지막 코드는 @Nick Surmanidze 덕분에 "post object field"("relation"이라는 이름의 "post object field")를 사용하여 다른 페이지에서 콘텐츠를 가져올 수 있습니다.마지막 줄을 어떻게 잡느냐의 문제만 남습니다.

<?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">&nbsp;</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를 통해 다른 페이지의 ROW가 있습니다.

                <?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">&nbsp;</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를 표시하는 것입니다.

  1. 홈 페이지의 페이지 ID를 나타내기 위해 사용자 정의 필드를 추가합니다.

  2. 이제 홈페이지 템플릿에 $otherPageId = get_field ('your_other_page_id')와 같은 것을 써야 합니다.

  3. 그러면 코드에 있는 것과 동일한 것을 실행할 수 있지만,

    have_rows ('selection')

function add second parameter

 have_rows('selection', $otherPageId)

어느 페이지에서 이 필드를 검색할 것인지를 나타내기 위해.

  1. 마지막 행을 얻기 위해서는 여러 가지 방법을 사용할 수 있습니다.하나는 배열에 행 콘텐츠를 할당한 다음 배열의 마지막 항목을 사용하는 것입니다. 또는 ACF 방식으로 수행하는 방법을 알려줄 수 있는 또 다른 토막이 있습니다.

$ repeater = get_field ('repeater')

$last_row = ($repeter);

echo $last_row['sub_field'];

언급URL : https://stackoverflow.com/questions/37722807/get-last-row-from-an-acf-flexible-content-and-display-it-on-another-page