bestsource

발견되지 않은 오류: WP_Post 유형의 개체를 어레이로 사용할 수 없습니다.

bestsource 2023. 2. 28. 23:39
반응형

발견되지 않은 오류: WP_Post 유형의 개체를 어레이로 사용할 수 없습니다.

템플릿 파일에 WordPress 메뉴를 배치하려고 했습니다.wp_nav_menu를 사용하는 것이 제 정적 HTML 템플릿과 호환되지 않는 마크업을 출력하기 때문에 불편함을 느꼈습니다.커스텀 메뉴 워커 클래스를 사용하여 wp_nav_menu 마크업을 변경할 수 있습니다.대신 메뉴 이름과 대응하는 URL만 사용하여 템플릿에 넣기를 원했습니다.이를 위해 기능을 사용하여 메뉴의 모든 속성/속성의 배열을 가져옵니다.

이 함수에 의해 생성된 코드는 다음과 같습니다.

I found it by using var_dump().

어레이([0] = WP_Post 객체([ID] = 20[post_author] = 1[post_date] = 2017-07-10 11:50:32[post_date_gmt] = 2017-07-10 10:50:32[post_content] =[post_module] = 개요[post_crypt] =[post_status] = 게시[closed_status] = 닫힘[ping_status] = 닫힘[post_password] =[post_name] = 정보[to_ping] =[ping] =[post_backets] = 2017-07-10 11:50:40[post_backet_gmt] = 2017-07-10 10:50:40[post_content_content] =[post_parent] = 0[http://localhost/mySite/] = http://localhost/?p=20[menu_order] = 1[post_type] = nav_menu_item[post_filen_type] =[count_count] = 0[filter] = 미가공[db_id] = 20[ menu _ item _ parent ]= 0[object_id] = 20[object] = 사용자 지정[type] = 사용자 지정[type_label] = 사용자 지정 링크[http] = 정보[url] = #[타깃] =[attr_filters] =[설명] =[parames] = 어레이([0] =)
[httpn] =)
[1] = WP_Post 객체([ID] = 21[post_author] = 1[post_date] = 2017-07-10 11:50:32[post_date_gmt] = 2017-07-10 10:50:32[post_content] =[post_does] = 할 수 있는 일[post_crypt] =[post_status] = 게시[closed_status] = 닫힘[ping_status] = 닫힘[post_password] =[post_name] = 할 수 있는 일[to_ping] =[ping] =[post_backets] = 2017-07-10 11:50:40[post_backet_gmt] = 2017-07-10 10:50:40[post_content_content] =[post_parent] = 0[http] = http://localhost/mySite/?p=21[menu_order] = 2[post_type] = nav_menu_item[post_filen_type] =[count_count] = 0[filter] = 미가공[db_id] = 21[ menu _ item _ parent ]= 0[object_id] = 21[object] = 사용자 지정[type] = 사용자 지정[type_label] = 사용자 지정 링크= 내가 할 수 있는 일[url] = #[타깃] =[attr_filters] =[설명] =[parames] = 어레이([0] =)
[httpn] =)
[2] = WP_Post 객체([ID] = 22[post_author] = 1[post_date] = 2017-07-10 11:50:32[post_date_gmt] = 2017-07-10 10:50:32[post_content] =[post_http] = 몇 가지 성과[post_crypt] =[post_status] = 게시[closed_status] = 닫힘[ping_status] = 닫힘[post_password] =[post_name] = a-communications-chievements[to_ping] =[ping] =[post_backets] = 2017-07-10 11:50:40[post_backet_gmt] = 2017-07-10 10:50:40[post_content_content] =[post_parent] = 0[http] = http://localhost/mySite/?p=22[menu_order] = 3[post_type] = nav_menu_item[post_filen_type] =[count_count] = 0[filter] = 미가공[db_id] = 22[ menu _ item _ parent ]= 0[object_id] = 22[object] = 사용자 지정[type] = 사용자 지정[type_label] = 사용자 지정 링크[http] = 몇 가지 성과[url] = #[타깃] =[attr_filters] =[설명] =[parames] = 어레이([0] =)
[httpn] =)
[3] = WP_Post 객체([ID] = 23[post_author] = 1[post_date] = 2017-07-10 11:50:32[post_date_gmt] = 2017-07-10 10:50:32[post_content] =[post_contact] = 연락처[post_crypt] =[post_status] = 게시[closed_status] = 닫힘[ping_status] = 닫힘[post_password] =[post_name] = 연락처[to_ping] =[ping] =[post_backets] = 2017-07-10 11:50:40[post_backet_gmt] = 2017-07-10 10:50:40[post_content_content] =[post_parent] = 0[http://localhost/mySite/] = http://localhost/?p=23[menu_order] = 4[post_type] = nav_menu_item[post_filen_type] =[count_count] = 0[filter] = 미가공[db_id] = 23[ menu _ item _ parent ]= 0[object_id] = 23[object] = 사용자 지정[type] = 사용자 지정[type_label] = 사용자 지정 링크[contact[url] = #[타깃] =[attr_filters] =[설명] =[parames] = 어레이([0] =)
[httpn] =)
)

내 의도는 'post_title'과 'url'의 값을 얻는 것이다.

내가 어떻게 그럴 수 있을까?

현재 이 코드 라인에 어려움을 겪고 있습니다.

function mt_get_menu_items($menu_name) {
if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {
    $menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
    return wp_get_nav_menu_items($menu->term_id);
}
}
$menu_items = mt_get_menu_items ('main_menu');
<nav id="nav">
<ul>
    <?php if (isset ($menu_items) ) : ?>
    <?php foreach ( (array) $menu_items as $key => $menu_item ) : ?>
    <?php // $menu_item_array = get_object_vars($menu_item[$key]);
                    $menu_object = $menu_item[$key];
                    // this line is 98
                 ?>
    <li>
         // this line is 101
        <a href="<?php $menu_object->url; ?>" class="active"><?php $menu_object->post_title; ?></a>
    </li>
    <?php endforeach; ?>
    <?php endif; ?>
</ul>
</nav>

이로 인해 다음 오류가 발생합니다.

치명적인 오류:미발견 오류: D:\xampp\htdocs\mySite\wp-content\temes\mySite\header에서 WP_Post 유형의 개체를 배열로 사용할 수 없습니다.php:98

이 답변은 참고용으로 여기에 올리겠습니다.

「」의 foreach

foreach ($originalArray as $key => $data) {
  $post_date = $data->post_date; // 2017-07-10 11:50:32
}

, 「」를 합니다.->.

::$data['post_date']로로 합니다.$data->post_date

개체를 검색하는 경우 다음과 같이 개체를 배열로 변환할 수 있습니다.

function object_to_array($obj) {
if(is_object($obj)) $obj = (array) $obj;
    if(is_array($obj)) {
        $new = array();
        foreach($obj as $key => $val) {
            $new[$key] = object_to_array($val);
        }
    }
    else $new = $obj;
    return $new;       
}

언급URL : https://stackoverflow.com/questions/45018459/uncaught-error-cannot-use-object-of-type-wp-post-as-array

반응형