Project request was to allow for multiple authors on each post. There are plugins that do this for standard installs of WordPress (WP). However, this client has a highly customized version of WP. The solution included using a plugin with custom code.
Previous Customization to Author
The client defines all staff in a custom post type called “people”. People includes the standard WP info plus additional fields that reference degrees and certifications, whether they are a writer and/or a presenter, and more.
Earlier this year, I was hired to add that post type and to replace the standard WP link to /authors/first-lastname/ to /people/first-lastname/ throughout the website.
The blog archive page, which displays the author underneath the post title, was customized to link to the person’s (people) bio instead of the author’s archive (more posts by this author) page.
The post’s (full) page, which also displays the author underneath the post title, was customized to do the same. In addition, a link was added at the bottom of each post; “more posts by Jane Doe”. This link was customized to work like the standard “author’s link” that is typically found in the post meta right underneath the post’s title.
For clarity: On the single page, the first mention of the author links to his/her bio page. The second mention links to “the author’s” archive page (all posts by this author).
Current WordPress Customization Request
Now, the client wants to be able to sometimes have one author and other times have two or even three authors AND have the same functionality as above where the first mention of each author links to his/her bio and the second mention in the footer of the single post links to the person’s archive of posts.
I’ve just completed making that happen.
Technical Solution
A plugin that prompts for multiple authors was installed (Co-Authors Plus). The client enters the author(s) name(s) on the right hand side of the (block) Dashboard.
Custom functions were created in the functions.php; one for displaying the authors’ bios and the other for displaying the authors’ posts.
The custom function to display the authors’ posts was actually provided by the vendors of the plugin, so, that was a cut-n-paste and test task. Easy peasy.
The custom function for the author’s bios was more complicated.
There is one other function provided by the plugin’s vendors. It lists the names of the authors without any links. I used that as a base. Instead of returning the results of that function,
- I loaded the results into an array
- Calculated how many first and last name combinations there are in order to know when to add commas and the word “and” in the text that will be returned as part of the content
- Then exploded the array into first and last names
- Searched each first and last name combo for apostrophes and replaced them with ” (nothing)
- Searched each first and last name combo for double quotes and replaced them with hyphens (some names include nicknames)
- Added a hyphen between the first and last name
- Wrapped each hyphened name in link tags (to the /people/ version not the usual /author/ link)
- Appended each linked name to a string variable
- And, returned that variable
So at the bottom of the post, there is a statement (with the real authors’ names) that reads like one of these:
More posts by Author One
More posts by Author One and Author Two
More posts by Author One, Author Two, and Author Three
And, in theory, there could be ten or more authors, and each would be strung together separated by commas, and with an ‘and’ before the last author.
It was an interesting little brain teaser and exercise that i enjoyed.