php - 如果一位作者有多篇文章,我如何只显示一位作者?

标签 php wordpress

我创建了自己的自定义帖子类型(文章)。现在,文章的所有作者都应该显示在概述页面上。如果单击姓名,您将进入相应的作者简介。

但是,如果一个作者写了例如三篇文章,那么他将在概述页面上显示三次。有谁知道我该如何解决这个问题?非常感谢。

<?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/

相关文章:

PHP 多个类的相同命名空间

php - 使用 json 显示来自 WordPress 的帖子提要不起作用

php - 如何使用javascript显示鼠标悬停在多个div上的div?

php - 在 Laravel 中测试 Controller 的正确方法是什么?

php - MySQL 中的理解断开连接

php - 安装包时向 .env 添加新的环境变量

php - Laravel 4 - 2 个具有相同输入名称的表单

wordpress - 更改发布日期时的 URL 重定向 - WordPress

jquery - 联系表 7 和默认单选值

jquery - 使父菜单可点击