programing

WordPress에서 게시물 작성자 이름을 얻는 방법

goodjava 2023. 2. 11. 09:24

WordPress에서 게시물 작성자 이름을 얻는 방법

WordPress에서 다음을 사용하여 게시물을 작성한 작성자 이름을 가져와야 합니다.author_id.
어떻게 찾을 수 있나요?author_name?

를 사용하여 작성자 데이터를 가져올 수 있습니다.

echo get_the_author_meta('display_name', $author_id);

이게 도움이 됐으면 좋겠네요!

이거 참 잘 될 거야

<?php echo get_the_author(); ?>

자세한 것은, 을 참조해 주세요.https://codex.wordpress.org/Function_Reference/get_the_author

아래 코드를 싱글로 사용합니다.php 또는 작성자 이름을 원하는 관련 페이지

<?php get_the_author_meta( 'display_name', $author_id ); ?>

WordPress Custom REST API 엔드포인트에서 사용하는 경우 다음과 같이 수행할 수 있습니다.

function customrestapiplugin_getpost( $slug ) {
    $args = [
        'name' => $slug['slug'],
        'post_type' => 'post'
    ];

    $post = get_posts($args);
    
    $data[$i]['id'] = $post[0]->ID;
        $data['title'] = $post[0]->post_title;
        $data['content'] = $post[0]->post_content;
        $data['excerpt'] = $post[0]->post_excerpt;
        $data['slug'] = $post[0]->post_name;
        $data['date'] = $post[0]->post_date;
        $data['link'] = get_permalink($post[0]->ID);
        $data['author'] = get_the_author_meta('display_name', $post[0]->post_author);
        $data['featured_image']['thumbnail'] = get_the_post_thumbnail_url($post[0]->ID, 'thumbnail');
        $data['featured_image']['medium'] = get_the_post_thumbnail_url($post[0]->ID, 'medium');
        $data['featured_image']['large'] = get_the_post_thumbnail_url($post[0]->ID, 'large');

    return $data;
}

이 코드를 단일 포스트에 추가합니다.php

<?php echo get_the_author(); ?>

이게 잘 됐으면 좋겠어!!

WordPress에서 게시물의 작성자를 구하는 방법에 대한 심리스한 솔루션을 찾고 있는 분들에게 이것은 또 다른 경량 방법입니다.

global $wpdb;

    $post_id = 12; // your post id
    
    $post_author_id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT post_author FROM {$wpdb->posts} WHERE ID = %d ", $post_id ) );
    
    $author =  new WP_User( $post_author_id );
    
    $display_name = $author->display_name;
    
    $avartar = get_avatar( $post_author_id, 30 ); // get_avatar( userid, size )
    
    $author_url = get_author_posts_url( $post_author_id );

언급URL : https://stackoverflow.com/questions/42871987/how-to-get-post-author-name-in-wordpress