我创建了自己的自定义帖子类型(文章)。现在,文章的所有作者都应该显示在概述页面上。如果单击姓名,您将进入相应的作者简介。
但是,如果一个作者写了例如三篇文章,那么他将在概述页面上显示三次。有谁知道我该如何解决这个问题?非常感谢。
<?php get_header();
$args = (array(
'post_type' => 'article',
'orderby' => 'name',
'order' => 'ASC',
));
$myquery = new WP_Query($args);
?>
<?php
// Start the loop.
if ($myquery->have_posts()) :
while ($myquery->have_posts()) :
$myquery->the_post();
$img = get_the_author_meta('image-author');
echo wp_get_attachment_image($img, 'image-author'); ?>
<?php echo the_author_posts_link($args); ?>
<?php
endwhile;
endif;
?>
<?php get_footer(); ?>
最佳答案
您可以尝试以下代码:
if ($myquery->have_posts()) :
$authors_ids = [];
while ($myquery->have_posts()) :
$myquery->the_post();
$img = get_the_author_meta('image-author');
$author_id = get_the_author_meta('ID');
if (in_array($author_id, $authors_ids)) {
continue;
}
$authors_ids[] = $author_id;
echo wp_get_attachment_image($img, 'image-author');
?>
<?php echo the_author_posts_link($args); ?>
<?php
endwhile;
endif;
关于php - 如果一位作者有多篇文章,我如何只显示一位作者?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60113745/