[英]How to get post author name in WordPress?
在 WordPress 中,我需要獲取使用author_id
創建帖子的作者的author_id
。
我怎樣才能找到author_name
?
這應該像魅力一樣工作
<?php echo get_the_author(); ?>
如需更多詳細信息。 https://codex.wordpress.org/Function_Reference/get_the_author
在 single.php 或您想要作者姓名的相關頁面中使用以下代碼
<?php get_the_author_meta( 'display_name', $author_id ); ?>
在 single-post.php 中添加此代碼
<?php echo get_the_author(); ?>
我希望這會奏效!!
在 WordPress 自定義 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;
}
對於那些正在尋找如何在 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 );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.