You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Co Author Plus实现带链接的作者信息单独分行展示?

问题说明

我正在开发一个项目,需要为文章添加带个人资料页面链接的作者姓名(例如作者1、作者2)。$author_post_links变量可实现带链接的作者展示,但它会将所有作者的链接连续排列在同一行。我需要让每位作者的信息单独分行展示,目前已有类似输出样式,但作者姓名缺失链接,以下是我的代码:

<?php
if (function_exists('coauthors_posts_links')) {
  $author_post_links = coauthors_posts_links( null, null, null, null, false );
} else {
  $author_post_links = '<a class="url fn n" href="' .
      esc_url( get_author_posts_url( $author_id ) ) .
      '">' . esc_html( get_the_author_meta( 'display_name', $author_id ) ) . '</a>';
}
$byline = sprintf(
  __('by %s'),
  '<span class="author vcard">' . $author_post_links . '</span>' );


$coauthors = get_coauthors();
if ( ! $coauthors ) {
  return;
}
foreach( $coauthors as $coauthor ) { 
 $avatar = coauthors_get_avatar( $coauthor, 150, '', $coauthor->display_name, '' );
 echo <<<EOT

  <li class="bottom-multi-author" >  
      <div class="bottom-multi-author-metabox" id="{$coauthor->ID}">
       <figure class="bottom-author-avatar">{$avatar}</figure>
       <div class="bottom-author-details">
       <h4 class="multi-author-name">{$coauthor->display_name} {$url}</h4>
       <h4 class="multi-author-nameewew">{$author_post_links}</h4>
       <h6 class="multi-author-designation">{$coauthor->author_designation}</h6>
       <p class="multi-author-bio">{$coauthor->description}</p>
      
    
       </div>
     
      </div>
      </li>
      
  EOT;
}
?></ul>

<?php 

?>
解决方案

问题核心是循环外生成的$author_post_links是所有作者的链接集合,而循环内需要单独处理每个作者的链接。直接在循环内为当前作者生成带链接的姓名即可:

  1. 移除循环内重复输出所有作者链接的无效代码行;
  2. 在循环内部为当前$coauthor生成专属链接,替换原有的作者姓名展示部分。

修改后的完整代码:

<?php
// 如需保留原行内作者展示可保留这段,否则直接删除
if (function_exists('coauthors_posts_links')) {
  $author_post_links = coauthors_posts_links( null, null, null, null, false );
} else {
  $author_post_links = '<a class="url fn n" href="' .
      esc_url( get_author_posts_url( $author_id ) ) .
      '">' . esc_html( get_the_author_meta( 'display_name', $author_id ) ) . '</a>';
}
$byline = sprintf(
  __('by %s'),
  '<span class="author vcard">' . $author_post_links . '</span>' );
// 可删除部分结束

$coauthors = get_coauthors();
if ( ! $coauthors ) {
  return;
}
foreach( $coauthors as $coauthor ) { 
 $avatar = coauthors_get_avatar( $coauthor, 150, '', $coauthor->display_name, '' );
 // 为当前作者生成带链接的姓名
 $author_link = sprintf(
    '<a class="url fn n" href="%s">%s</a>',
    esc_url( get_author_posts_url( $coauthor->ID ) ),
    esc_html( $coauthor->display_name )
 );
 echo <<<EOT

  <li class="bottom-multi-author" >  
      <div class="bottom-multi-author-metabox" id="{$coauthor->ID}">
       <figure class="bottom-author-avatar">{$avatar}</figure>
       <div class="bottom-author-details">
       <h4 class="multi-author-name">{$author_link}</h4>
       <h6 class="multi-author-designation">{$coauthor->author_designation}</h6>
       <p class="multi-author-bio">{$coauthor->description}</p>
       </div>
      </div>
      </li>
      
  EOT;
}
?></ul>

关键修改点

  • 在foreach循环内新增$author_link变量,针对当前作者生成专属的带链接姓名,确保每个链接正确指向对应作者的个人页面;
  • 移除了循环内重复输出所有作者链接的<h4 class="multi-author-nameewew">{$author_post_links}</h4>行,避免冗余内容。

内容的提问来源于stack exchange,提问作者Aurangzaib Khan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 21:47:41