programing

사용자 지정 게시 유형의 사용자 지정 데이터를 표시하는 방법

newnotes 2023. 3. 11. 09:25
반응형

사용자 지정 게시 유형의 사용자 지정 데이터를 표시하는 방법

커스텀 투고 타입을 작성했습니다.Wordpress 대시보드에 정상적으로 로딩되어 저장도 가능합니다.몇 개의 문자열과 몇 개의 날짜에 대한 데이터가 포함된 사용자 지정 게시 유형이라고 가정합니다.

이러한 커스텀 투고 타입(WP_Query를 사용하여 post_type을 커스텀 투고 타입의 이름으로 지정)을 취득할 수 있도록 하고 싶다.반환된 오브젝트에서 print_r을 호출하면 오브젝트 어디에도 커스텀데이터(스트링과 날짜)가 저장되어 있지 않습니다.데이터베이스에서 어떻게 검색하면 될까요?

몇 시간 동안 둘러보았지만 이 데이터를 검색할 방법을 찾지 못했습니다.

요청하신 대로:데이터는 다음과 같이 저장됩니다.

function update_obituary(){
    global $post;
    update_post_meta($post->ID, "first_name", $_POST["first_name"]);
    update_post_meta($post->ID, "last_name", $_POST["last_name"]);
    update_post_meta($post->ID, "birth_date", $_POST["birth_date"]);
    update_post_meta($post->ID, "death_date", $_POST["death_date"]);
    update_post_meta($post->ID, "publication_date", $_POST["publication_date"]);
}

이 함수는 'save_post' 후크에 연결되어 있습니다.커스텀 포스트 타입 인스턴스를 편집 모드로 다시 열면 데이터가 다시 표시됩니다.데이터베이스에 저장되어 있다는 거죠?

해당 유형의 게시물을 편집할 때 메타데이터가 표시되는 경우, 해당 메타데이터가 DB에 성공적으로 저장되어 있어야 합니다.

커스텀 투고 타입의 메타데이터를 취득하기 위한 wp 함수는 와 두 가지가 있습니다.다른 점은요.get_post_custom_values는, 1 개의 키에 관련지어져 있는 복수의 값을 가지는 커스텀 필드 등, 유저가 아닌 커스텀 필드에 액세스 할 수 있습니다.취향의 문제라는 독특한 분야에서도 사용할 수 있습니다.

투고 타입이 「부고」라고 하는 것을 전제로 합니다.

// First lets set some arguments for the query:
// Optionally, those could of course go directly into the query,
// especially, if you have no others but post type.
$args = array(
    'post_type' => 'obituary',
    'posts_per_page' => 5
    // Several more arguments could go here. Last one without a comma.
);

// Query the posts:
$obituary_query = new WP_Query($args);

// Loop through the obituaries:
while ($obituary_query->have_posts()) : $obituary_query->the_post();
    // Echo some markup
    echo '<p>';
    // As with regular posts, you can use all normal display functions, such as
    the_title();
    // Within the loop, you can access custom fields like so:
    echo get_post_meta($post->ID, 'birth_date', true); 
    // Or like so:
    $birth_date = get_post_custom_values('birth_date');
    echo $birth_date[0];
    echo '</p>'; // Markup closing tags.
endwhile;

// Reset Post Data
wp_reset_postdata();

혼동을 피하기 위한 주의사항:에서의 부울 생략get_post_meta문자열이 아닌 배열을 반환하도록 합니다. get_post_custom_values항상 배열을 반환하기 때문에 위의 예에서는 에코는$birth_date[0],보다는$birth_date.

그리고 지금으로선 100% 확신할 수 없지만$post->ID상기의 예상대로 동작합니다.그렇지 않은 경우 로 교체합니다.get_the_ID()둘 다 효과가 있을 거예요. 확실히 한 사람이 할 거예요.시험해 볼 수는 있지만 시간을 절약하면...

완전성을 위해 더 많은 쿼리 인수와 올바른 사용법에 대해 코덱스를 확인하십시오.

언급URL : https://stackoverflow.com/questions/8015217/how-to-display-custom-data-from-custom-post-types

반응형