- This topic has 10 replies, 3 voices, and was last updated 3 years ago by
David.
-
AuthorPosts
-
March 12, 2023 at 9:43 pm #2565408
Alfredo
I created a Custom Post Type for authors with some fields from ACF. The reason for creating a CPT instead of using default WordPress users are:
– I need a custom template for the author’s page different than the one that comes by default with WordPress users (as I understand is not easy to modify)
– These authors don’t upload the post themselves as the editor team does that (so they are not really users)
– For the authors page I need simple data: photo, name, position and description (more simple than users data and achievable with ACF)
– Posts may have multiple authors
– I need an “All authors” page with nice formatting and filters by first letter of names (not sure if this can be done with default users)Then I created a post object with ACF to establish the relation between blog posts and CPT authors.
But now I’m encountering difficulties trying to pull dynamic data (like author description) in the content template for blog posts. Is there a clean way for doing this? Would be easier if I just stick to default WordPress users and all the things I’m trying to do with CPT are doable with default users?
I’m using GB pro but I’m not able to pull data from a post_object in Block – Content Template
March 12, 2023 at 10:41 pm #2565444Fernando Customer Support
Hi Alfredo,
To clarify, do CPT authors have posts from your site as well?
If so, it might be easier indeed to just use the default user system. Just create a new role if needed.
Then, you can use ACF to add other meta data.
You can retrieve simple meta data through GB Headline Blocks.
March 13, 2023 at 12:20 am #2565508Alfredo
Thanks for your fast answer Fernando!
I’m not sure if I understand your answer. CPT authors are the authors of the different blog posts. All the blog post have one or multiple authors from the CPT authors.
Regarding default user system, I’ve seen I could edit the author page by creating an author.php file. Is there any way of editing that page with a Block – content template and GB?
March 13, 2023 at 12:24 am #2565515Fernando Customer Support
It’s easier to use the default system if that’s the case.
What you can use is a Loop Template: https://docs.generatepress.com/article/block-element-loop-template/
You can add a Query Loop to Query the author’s posts, and then you can add other stuff like author meta through this.
March 13, 2023 at 3:34 am #2565731Alfredo
Let’s say I go for the user default system as you suggest. I can create some ACF to add more data to the default WordPress user. And then create with ACF a relationship field – user in blog posts for adding co-authors when needed.
But how can I tackle the issue described in the first post when there are multiple authors? I mean this:
Pulling dynamic data in the content template of blog post for the second author (in order to create a second author box). For example, pulling the charge of the author which is a Custom Field. I’m not able to make this with GB Pro.
March 13, 2023 at 5:42 am #2565851Alfredo
I’ve found another way of assigning multiple authors to a post with Co-author plus plugin which was suggested by GP support team in other threads (just in case this method is more convenient)
But still can’t figure out how to pull the custom fields data from the second author in order to create a second author box in the blog post content template. I guess “author meta” does not apply here as this pulls data from the first author.
March 13, 2023 at 5:00 pm #2566610Fernando Customer Support
You’ll need to reach out to the plugin developer if there are any solutions for this.
One thing I could think of is to add a post meta that stores the name of the co-author, and then use this name to retrieve the co-author ID. Reference: https://rudrastyh.com/wordpress/get-user-id.html#user_id_by_name
Then, use this ID when getting your custom user meta: https://developer.wordpress.org/reference/functions/get_user_meta/
March 14, 2023 at 3:48 am #2567039Alfredo
Could you elaborate more on how to achieve that?
I’ve managed to display multiple authors in the default blog post thanks to this code: https://generatepress.com/forums/topic/showing-multiple-authors-using-co-authors-plus-plugin/#post-732801
But I would like to retrieve the co-author data (name, image, bio) for adding author boxes dynamically to the content template I’m creating for the blog posts.
I’ve found this code which I think makes something similar to what I want to achieve:
global $post; $author_id=$post->post_author; foreach( get_coauthors() as $coauthor ): ?> <div class="entry-author co-author"> <?php echo get_avatar( $coauthor->user_email, '96' ); ?> <h3 class="author vcard"><span class="fn"><a href="<?php echo get_author_posts_url( $coauthor->ID, $coauthor->user_nicename ); ?>"><?php echo $coauthor->display_name; ?></a></span></h3> <p class="author-bio"><?php echo $coauthor->description; ?></p> <div class="clear"></div> </div><!-- .entry-author co-author --> <?php endforeach;But I’m not sure where I would need to insert it. I tried in the functions.php from the child theme and nothing happens. Where can I find the file for the template I’m creating with GB?
March 14, 2023 at 7:12 am #2567239David
StaffCustomer SupportHi there,
you would need to create a shortcode function like this:
function db_show_coauthors() { global $post; // Check if the Co-Authors Plus plugin is active if (!function_exists('get_coauthors')) { return ''; } // Get an array of coauthors for the current post $coauthors = get_coauthors($post->ID); // Initialize an empty string to store the HTML output $output = ''; // Loop through the array of coauthors and generate HTML output for each one foreach ($coauthors as $coauthor) { // Generate HTML output for the coauthor using sprintf $output .= sprintf( '<div class="author-item"> <div class="author-avatar">%s</div> <div class="author-name"><a href="%s">%s</a></div> <div class="author-description">%s</div> </div>', get_avatar($coauthor->ID, 64), get_author_posts_url($coauthor->ID), $coauthor->user_nicename, $coauthor->description ); } // Return the HTML output return $output; } add_shortcode('co_author_boxes','db_show_coauthors');Then you can add the
[co_author_boxes]shortcode to your content template.March 14, 2023 at 6:27 pm #2567949Alfredo
It works! Thank you very much!! I’ve been struggling with this long hours!
Just in case it can help someone:
Based on David’s code I’ve managed to add to the author boxes:
– A text custom field (position)
– An author image different than the gravatar (with customs fields)This is the code:
function db_show_coauthors() { global $post; // Check if the Co-Authors Plus plugin is active if (!function_exists('get_coauthors')) { return ''; } // Get an array of coauthors for the current post $coauthors = get_coauthors($post->ID); // Initialize an empty string to store the HTML output $output = ''; // Loop through the array of coauthors and generate HTML output for each one foreach ($coauthors as $coauthor) { // Get the author image URL $author_image_url = get_field('author-image', 'user_' . $coauthor->ID); // Generate HTML output for the coauthor using sprintf $output .= sprintf( '<div class="author-item"> <div class="author-avatar"><img src="%s" alt="%s" width="64" height="64" /></div> <div class="author-name"><a href="%s">%s</a></div> <div class="author-custom-fields-position">%s</div> <div class="author-description">%s</div> </div>', !empty($author_image_url) ? $author_image_url : get_avatar_url($coauthor->ID), $coauthor->display_name, get_author_posts_url($coauthor->ID), $coauthor->user_nicename, get_field('position', 'user_' . $coauthor->ID), // Replace "custom_field" with the name of your ACF field $coauthor->description ); } // Return the HTML output return $output; } add_shortcode('co_author_boxes','db_show_coauthors');March 15, 2023 at 4:33 am #2568358David
StaffCustomer SupportSuper glad to hear that. And thanks for sharing your solution!
-
AuthorPosts
- You must be logged in to reply to this topic.