<?php
function authors_editors_shortcode() {
$post_id = get_the_ID();
$original_author_id = get_post_field('post_author', $post_id);
$original_author = get_the_author_meta('display_name', $original_author_id);
$original_author_url = get_author_posts_url($original_author_id);
$coauthors = get_field('coauthors', $post_id);
$content = '';
if (!$coauthors) {
$content .= "Written by: <a href='{$original_author_url}'>{$original_author}</a>";
} else {
$authors = array("<a href='{$original_author_url}'>{$original_author}</a>");
$editors = array();
if (have_rows('select_coauthors', $post_id)) {
while (have_rows('select_coauthors', $post_id)) {
the_row();
$type = get_sub_field('type');
$user = get_sub_field('select_user');
if ($user) {
$user_name = $user['display_name'];
$user_url = get_author_posts_url($user['ID']);
$user_link = "<a href='{$user_url}'>{$user_name}</a>";
if ($type === 'author') {
$authors[] = $user_link;
} elseif ($type === 'editor') {
$editors[] = $user_link;
}
}
}
}
$content .= "Written by: " . implode(', ', $authors);
if (!empty($editors)) {
$content .= "<br>Edited by: " . implode(', ', $editors);
}
}
return '<div id="authors-editors">' . $content . '</div>';
}
add_shortcode('authors_editors', 'authors_editors_shortcode');