programing

get_page_children () 일부 하위 페이지를 반환하지 않음

megabox 2023. 10. 21. 10:18
반응형

get_page_children () 일부 하위 페이지를 반환하지 않음

제 워드프레스 테마에는 어린이 페이지에 정보를 표시하는 섹션이 있습니다.이것이 지금 제가 가지고 있는 것입니다.

<?php 
                $my_wp_query = new WP_Query();
                $all_wp_pages = $my_wp_query->query(array('post_type' => 'page'));

                $staff = get_page_children(8, $all_wp_pages);

                foreach($staff as $s){
                    $page = $s->ID;
                    $page_data = get_page($page);
                    $content = $page_data->post_content;
                    $content = apply_filters('the_content',$content);
                    $content = str_replace(']]>', ']]>', $content);
                    echo '<div class="row-fluid"><span class="span4">'; 
                    echo get_the_post_thumbnail( $page ); 
                    echo '</span><span class="span8">'.$content.'</span></div>';
                } 
        ?>

아이 페이지가 5개나 있는데 3개만 돌아옵니다.$staff에 print_r을 사용하여 다른 페이지가 배열되어 있는지 확인했지만 그렇지 않습니다.무엇이 문제인지 잘 모르겠습니다.

아무 문제 없습니다.get_page_children()아니면new WP_Query(). 기본적으로WP_Query마지막만 반환합니다.x작성된 페이지 수입니다.그것은 그들에게 부과된 제한입니다.WP_Query.

get_page_children()단순히 반환되는 페이지 배열을 가져옵니다.WP_Query목록에서 자식 페이지를 필터링합니다.WordPress Codex: get_page_children에 따르면 "... 자식을 가져오기 위해 SQL 쿼리를 만들지 않습니다."

문제를 해결하려면 다음을 사용하기만 하면 됩니다.

    $query = new WP_Query( 'posts_per_page=-1' );

수정 사항이 포함된 코드:

    <?php 
    $my_wp_query = new WP_Query();
    $all_wp_pages = $my_wp_query->query(array('post_type' => 'page', 'posts_per_page' => -1));

    $staff = get_page_children(8, $all_wp_pages);

    foreach($staff as $s){
        $page = $s->ID;
        $page_data = get_page($page);
        $content = $page_data->post_content;
        $content = apply_filters('the_content',$content);
        $content = str_replace(']]>', ']]>', $content);
        echo '<div class="row-fluid"><span class="span4">'; 
        echo get_the_post_thumbnail( $page ); 
        echo '</span><span class="span8">'.$content.'</span></div>';
    } 
    ?>

페이지 자녀를 구해야 할 때 언제든지 전화할 수 있는 도우미 기능이 있습니다.

    function my_get_page_children( $page_id, $post_type = 'page' ) {
        // Set up the objects needed
        $custom_wp_query = new WP_Query();
        $all_wp_pages    = $custom_wp_query->query( array( 'post_type' => $post_type, 'posts_per_page' => -1 ) );

        // Filter through all pages and find specified page's children
        $page_children = get_page_children( $page_id, $all_wp_pages );

        return $page_children;
    }

도우미 기능으로 코딩합니다.

    foreach(my_get_page_children(8) as $s){
        $page = $s->ID;
        $page_data = get_page($page);
        $content = $page_data->post_content;
        $content = apply_filters('the_content',$content);
        $content = str_replace(']]>', ']]>', $content);
        echo '<div class="row-fluid"><span class="span4">'; 
        echo get_the_post_thumbnail( $page ); 
        echo '</span><span class="span8">'.$content.'</span></div>';
    } 

저도 비슷한 문제가 있었어요 - get_page_children들이 이상하게 행동하는 것 같아요...(제 경우에는 3명의 자녀가 있는 한 페이지에 대해서는 3명을, 4명이 있는 다른 페이지에 대해서는 0명을 반환했습니다! - 해결할 수 없습니다..

대신 사용자 지정 쿼리를 사용하여 해결했습니다.

$params = array(
'post_type'=>'page',
'post_parent'=> 8,
);
$staff = query_posts($params);

유사한 내용: http://www.sanraul.com/2010/08/28/get-page-children/

참고: 사용 위치에 따라 필요할 수 있습니다.wp_reset_query();또는 그 외의 것query_posts()메인 루프가 깨질 수도 있어요!

그게 도움이 되길! - A

$curID = get_the_ID();

query_posts(
    array(
        'post_type'=>'page',
        'post_parent'=>$curID,
        'orderby' => 'menu_order',
        'order' => 'ASC'
    )
);

echo '<ul>';
while ( have_posts() ) : the_post();
    echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
endwhile;
echo '</ul>';

wp_reset_query();

아래 편집 참조

방금 이 문제를 경험하고 해결한 후에, 제가 취해야 할 접근 방식을 공유하려고 생각했습니다.

원래는 새로운 WP_Query를 사용하여 모든 페이지를 잡은 다음 쿼리 결과를 get_page_children()에 입력했습니다.

$all_pages = new WP_Query( array( 'post_type' => 'page' ) );
wp_reset_postdata();
$page_id = get_the_ID();

// pass $all_pages as the second parameter
$page_children = get_page_children( $page_id, $all_pages );

foreach ( $page_children as $childObj ) {
    echo '<a href="' . $childObj->guid . '">' . $childObj->post_title . '</a>'
}

어떤 이유에서건 위 코드는 저에게 통하지 않았습니다.

대신 다음과 같이 $all_pages의 'posts' 개체를 두 번째 매개 변수로 전달했습니다.

$all_pages = new WP_Query( array( 'post_type' => 'page' ) );
wp_reset_postdata();
$page_id = get_the_ID();

// pass the posts object of $all_pages as the second parameter
$page_children = get_page_children( $page_id, $all_pages->posts );

foreach ( $page_children as $childObj ) {
    echo '<a href="' . $childObj->guid . '">' . $childObj->post_title . '</a>';
}

누군가에게 도움이 되었으면 좋겠습니다!


편집: get_page_children() 함수에 더 많은 문제가 발생한 후, 저는 동일한 최종 결과를 얻기 위해 지속적으로 작동해 온 다른 경로를 선택합니다.

$page = get_queried_object();
$children = get_pages( 'child_of=' . $page->ID . '&parents=' . $page->ID );

// loop through the child page links
foreach ( $children as $child ) {
    echo '<a href="' . get_permalink( $child->ID ) . '">' . get_the_title( $child->ID ) . '</a>';
}

간단히 사용할 수 있습니다.

$args = array(
        'post_parent' => $parent_id ,
        'post_type'   => 'page', 
        'numberposts' => -1,
        'post_status' => 'publish' 
    );
    $children = get_children( $args );

$children 변수는 부모 ID의 모든 자식 페이지에 제공됩니다. WP_Query를 사용하는 경우 자세한 정보를 보려면 코드별로 쿼리를 재설정해야 합니다. https://developer.wordpress.org/reference/functions/get_page_children/

당신의 기능에 이 코드를 사용합니다.php 파일 및 부모 페이지 ID의 호출 함수

    function load_clildren( $parent_id=null ){
    $parent_id  =   ( $parent_id ) ? $parent_id : $post->ID;

    $args = array(
        'post_parent' => $parent_id ,
        'post_type'   => 'page', 
        'numberposts' => -1,
        'post_status' => 'publish' 
    );
    $children = get_children( $args );
    return $children;
}

함수 호출 방식

$ch_pages   =   load_clildren( $parent_id );

또는 상위 페이지에서 사용

$ch_pages   =   load_clildren();

언급URL : https://stackoverflow.com/questions/14224089/get-page-children-not-returning-all-child-pages

반응형