Archived topic
Adding Co-Author in Post MetaData
16 replies · Started by KC on May 13, 2021
Hello,
I bought the GeneratePress Premium version. I am also using a plugin called "Simple Author Box Premium" by WebFactory.
May I ask how do I add my co-author name underneath the post title?
I tried reading many forum posts and searching up how to do it, but most of them are for "CoAuthor Plus" plugin:
https://generatepress.com/forums/topic/showing-multiple-authors-using-co-authors-plus-plugin/
Can you help me?
Hi there,
we would need to know how the Plugin is adding the co-author data. Check with their support to see what function they provide to get that info.... once we know we can help you add it to your site.
Hello,
Thank you so much. I emailed the plugin's creator and got this response:
We do not have a public dedicated function,
please use $co_authors = get_post_meta($post->ID, 'sabox-coauthors', true); to get the guest author IDs.
Hi Klause,
Assuming you've already assigned co-authors, we can hook the variable the plugin's creator provided after the post title using a hook element.
Here's what you can do:
1.) Create a Hook Element.
2.) Add this code on its code area
<?php
$co_authors = get_post_meta($post->ID, 'sabox-coauthors', true);
echo $co_authors;
?>
3.) Set the display rule location to "post - all posts".
4.) Make sure "Execute PHP" is checked.
5.) Set the hook to "generate_after_entry_title"
6.) Save the hook element.
A string should appear after your post title. If you wish to modify the HTML tag of the string, modify the code given on #2.
Hi Elvin,
Thank you so much for your assistance.
Unfortunately, it doesn't work. No output or anything.
I made a slight change to Elvins code - can you try that.
Hi David, thank you for your response too.
Strangely, there's no output at all. Cleared my cache and test again and even on different computers/devices, still no output.
I've also emailed the plugin developer again to inquire too. Very odd that there's no output.
By the way, as I'm studying the plugin's code, I found this line which I think may be useful?
public function display_author_meta_box( $post ) {
global $user_ID;
?>
<div id="sab-coauthors">
<?php
wp_nonce_field( 'sabox-add-co-authors', 'sabox-co-authors-nonce' );
$coauthors = get_post_meta( $post->ID, 'sabox-coauthors', true );
if ( ! empty( $coauthors ) ) {
foreach ( $coauthors as $coauthor_id ) {
$coauthor = get_userdata( $coauthor_id );
echo '<div class="sab-co-author"><input type="hidden" name="sabox-coauthors[]" value="' . $coauthor_id . '"><span>' . $coauthor->display_name . ' ( ' . $coauthor->user_login . ' )' . '</span><span class="dashicons dashicons-no"></span></div>';
}
}
?>
</div>
<div class="sabox-coauthors-container">
<?php
$users = get_users();
echo '<select name="" id="sabox-co-authors" class="selectized" tabindex="-1" >';
foreach ( $users as $user ) {
echo '<option value="' . esc_attr( $user->id ) . '" >' . $user->display_name . ' ( ' . $user->user_login . ' )</option>';
}
echo '</select>';
submit_button( __( 'Add Guest Author', 'saboxpro' ), 'primary', '', true, array( 'id' => 'sabox-add-guest-author' ) );
?>
</div>
<?php
}
and...
public function show_guest_author( $sabox_options ) {
global $post;
if ( isset( $sabox_options['enable_guest_authors'] ) && $sabox_options['enable_guest_authors'] != '0' ) {
$template = Simple_Author_Box_Helper::get_template( 'template-guest-author.php' );
$co_authors = get_post_meta( $post->ID, 'sabox-coauthors', true );
$sabox_guest_author = false;
if ( is_array( $co_authors ) && ! empty( $co_authors ) ) {
if ( isset( $sabox_options['co_authors'] ) && '0' != $sabox_options['co_authors'] ) {
if ( isset( $sabox_options['co_authors_custom'] ) && $sabox_options['co_authors_custom'] == '1' ) {
echo '<h2 class="sabox-guest-authors">' . esc_html( $sabox_options['co_authors_custom_text'] ) . '</h2>';
} else {
echo '<h2 class="sabox-guest-authors">' . esc_html__( 'Co Authors :', 'saboxpro' ) . '</h2>';
}
} else {
$sabox_guest_author = true;
}
echo "<div class='sabox-guest-authors-container'>";
foreach ( $co_authors as $co_author ) {
$sabox_author_id = $co_author;
include( $template );
}
echo "</div>";
}
}
}
Can you share a link to a post where the co-author text should be displayed?
Hi David,
Thank you so much.
I wish to have privacy for the website so I don't wish to publish the link here.
I've sent an email via the contact form. The title of the email is the same as this thread's title.
Thank you.
In your hook element ie this code:
<?php
$co_authors = get_post_meta($post->ID, 'sabox-coauthors', true);
echo $co_authors;
?>
Can you try a simple test by adding some simply HTML after the PHP eg.
<?php
$co_authors = get_post_meta($post->ID, 'sabox-coauthors', true);
echo $co_authors;
?>
<p>I should be displayed</p>
Does the line of text get displayed ? If it does then we know its not the hook condition but the get_post_meta not returning a value.
One thing to try is replacing $post->ID with get_the_ID(), as $post isn't defined in the above snippets.
So it would look like this:
<?php
$co_authors = get_post_meta(get_the_ID(), 'sabox-coauthors', true);
echo $co_authors;
?>
Thank you all for your responses!
David:
<?php
$co_authors = get_post_meta($post->ID, 'sabox-coauthors', true);
echo $co_authors;
?>
<p>I should be displayed</p>
This code displays the output "I should be displayed" in all posts... Not just the one that has the co-author, but all (even ones without co-authors).
Tom:
<?php
$co_authors = get_post_meta(get_the_ID(), 'sabox-coauthors', true);
echo $co_authors;
?>
For the specific post that has co-author, the output message was just "Array".
Ah, I figured it out after Tom's suggested get_the_ID(), and I played around and got the output.
Thanks again for the help everyone!
Oh and one more thing...
How do I make that output appear in the same line as the post meta data?
So for example, it shoud look like:
“by [author] | [month, day, year] | [co-author info here]
All in the same line.