在我的分类页面上,未显示文章作者姓名
这就是我正在使用的
<?php echo get_the_author_meta(\'display_name\', $author_id); ?>
这是我的循环
<div class="a-article-wrapper row">
<?php
$cat_name = \'category\';
$categories = get_the_terms( $post->ID, $cat_name );
$top_level_terms = get_the_terms( $post->ID, $cat_name );
foreach( $top_level_terms as $top_term )
{
if(!($top_term->parent))
{
$child_terms = get_terms( $cat_name, array( \'child_of\' => $top_term->term_id ));
//Parent Category Name
//echo \'<div class="maincat-HC">\'.$top_term->name .\'</div>\';
$top_id=$top_term->term_id;
if(count($child_terms)>0)
{
foreach ( $child_terms as $child_term )
{
$id=$child_term->term_id;
//Child category name
//echo \'<ul class="hc-accordion"><li><div class="hc-subcat">\' .$child_term->name .\'</div>\';
$myposts=get_posts(array(
\'post_type\' => \'post\',
\'nopaging\' => true,
\'tax_query\' => array(
array(
\'taxonomy\' => $cat_name,
\'order\' => \'ASC\',
\'field\' => \'term_id\',
\'terms\' => $id)
))
);
foreach ($myposts as $mypost) { ?>
<div class="col-md-4 a-article-row">
<?php
echo get_the_author_meta(\'display_name\', $author_id);
?>
<?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($mypost->ID), \'full\' );?>
<?php if(has_post_thumbnail()) { ?>
<div class="archive-thumb-img" style="background-image: url(\'<?php echo $thumb[\'0\'];?>\')"></div>
<?php } else { ?>
<div class="archive-thumb-img"></div>
<?php } ?>
<div class="article-detail">
<div class="category">
<?php
foreach($categories as $category) {
if($category->parent){
echo $category->name;
}
}
?>
</div>
<div class="a-article-title">
<a href="<?php the_permalink($mypost);?>" title="<?php the_title(); ?>">
<?php echo mb_strimwidth( get_the_title(), 0, 45, \'...\' ); ?>
</a>
</div>
<div class="author">
<?php echo get_the_author_meta(\'display_name\', $author_id); ?>
</div>
</div>
</div>
<?php } wp_reset_postdata();
}
}
}
}
?>
</div>
最合适的回答,由SO网友:Jrod 整理而成
您需要正确地传入作者ID。
该行:
<?php echo get_the_author_meta(\'display_name\', $author_id); ?>
应该是这样的:
<?php echo get_the_author_meta( \'display_name\', $mypost->post_author ); ?>
或者,您可以使用
setup_postdata()
在foreach循环中,设置全局$post变量并使用模板标记函数,而无需传入ID。
global $post;
foreach ( $myposts as $post ) { setup_postdata( $post ); ?>
<div class="col-md-4 a-article-row">
<?php
echo get_the_author_meta(\'display_name\');
?>
<?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($mypost->ID), \'full\' );?>
<?php if(has_post_thumbnail()) { ?>
<div class="archive-thumb-img" style="background-image: url(\'<?php echo $thumb[\'0\'];?>\')"></div>
<?php } else { ?>
<div class="archive-thumb-img"></div>
<?php } ?>
<div class="article-detail">
<div class="category">
<?php
foreach($categories as $category) {
if($category->parent){
echo $category->name;
}
}
?>
</div>
<div class="a-article-title">
<a href="<?php the_permalink($mypost);?>" title="<?php the_title(); ?>">
<?php echo mb_strimwidth( get_the_title(), 0, 45, \'...\' ); ?>
</a>
</div>
<div class="author">
<?php echo get_the_author_meta(\'display_name\', $author_id); ?>
</div>
</div>
</div>
<?php } wp_reset_postdata();