显示作者姓名,而不是作者ID

时间:2013-12-28 作者:aled2305

我添加了获取作者的代码,但它显示了作者的ID号。我怎样才能得到作者的名字和他们的坟墓。

CODE

<?php
$product_pages_args = array(
    \'meta_key\' => \'_wp_page_template\',
    \'meta_value\' => \'page_library_html_content.php\',
    \'hierarchical\' => \'0\'
);
$product_pages = get_pages( $product_pages_args );
?>

<?php
foreach ( $product_pages as $product_page ) {
    echo \'<div id="posts" class="flex_100">\';
    echo \'<div id="library_title"><a href="\' . get_permalink( $product_page->ID ) . \'">\' . $product_page->post_title  . \'</a></div>\';
    echo \'<div class="library-content">\' . get_post_field(\'post_content\', $product_page->ID ) . \'</div>\';
    echo \'<div class="library-author">\' . get_post_field(\'post_author\', $product_page->ID ) . \'</div></div>\';
}
?>

2 个回复
最合适的回答,由SO网友:Subharanjan 整理而成
<?php
$product_pages_args = array(
    \'meta_key\' => \'_wp_page_template\',
    \'meta_value\' => \'page_library_html_content.php\',
    \'hierarchical\' => \'0\'
);
$product_pages = get_pages( $product_pages_args );
?>

<?php
foreach ( $product_pages as $product_page ) {
    $author_id =  get_post_field(\'post_author\', $product_page->ID );
    $author_details = get_user_by( \'id\', $author_id );
    $author_name = $author_details->first_name . \' \' . $author_details->last_name;  

    echo \'<div id="posts" class="flex_100">\';
    echo \'<div id="library_title"><a href="\' . get_permalink( $product_page->ID ) . \'">\' . $product_page->post_title  . \'</a></div>\';
    echo \'<div class="library-content">\' . get_post_field(\'post_content\', $product_page->ID ) . \'</div>\';
    echo \'<div class="library-author">\' . $author_name . \'</div>\';
    echo get_avatar( $author_id , 32 );
    echo \'</div>\';
}
?>
SO网友:Will

既然你现在有了作者的ID,你可以使用get_the_author_meta 要知道他们的名字。借用上述代码:

$author_id = get_post_field(\'post_author\', $product_page->ID ); 

//get the author\'s display name 
$author_name = get_the_author_meta( \'display_name\', $author_id ); 
echo \'<div class="library-author">\' . $author_name . \'</div>\';
而且,由于您有作者id,因此可以使用

get_avatar( $author_id );

它返回一个img元素。您必须在适当的位置进行回音,例如:

echo get_avatar( $author_id );
将导致<img src="http://some-url.com/some-image.gif">

请参阅get\\u avatar的文档,以查看可以传递给它的可选参数。http://codex.wordpress.org/Function_Reference/get_avatar

结束

相关推荐

未在循环外工作的_Author()

索引中有以下代码。php和我希望包含这行代码(下面的代码)以在循环顶部显示作者信息。 <?php include \'author-top.php\'; ?><!--author--> 我希望它显示在标题后面,如下所示<?php get_header(); ?> <!--This is where I want my author div to display--> <?php include \'author-top.php