• Resolved wprajshukla

    (@wprajshukla)


    Hi Jones 🙂

    I have created a section to fetch guest authors name, image(avatar/custom) and bio to fetch below my blog post article, but unfortunately only name is coming accurately, can you please suggest what should I do in order to fetch all accurately. Thanks : )
    Here is the code I tried

    <?php
    						global $post;
    
    						$post_author_name = '';
    
    						if (function_exists('sgan_get_guest_author_name')) {
    							$guest_author_name = sgan_get_guest_author_name($post->ID);
    							if (!empty($guest_author_name)) {
    								$post_author_name = $guest_author_name;
    							}
    						}
    
    						if (empty($post_author_name)) {
    							$post_author_id = $post->post_author;
    							$post_author_data = get_userdata($post_author_id);
    							if ($post_author_data) {
    								$post_author_name = get_the_author($post_id);
    							}
    						}
    
    						$author_bio = '';
    
    						if (!empty($post_author_id)) {
    							$author_bio = get_the_author_meta('description', $post_author_id);
    						}
    
    						if (!empty($post_author_name)) {
    							$author_image_url = get_avatar_url($post_author_id);
    						?>
Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Author A. Jones

    (@nomadcoder)

    Here you go:

    Authorname

    get_post_meta( $post->ID, ‘sfly_guest_author_names’, true ));

    Email Address

    get_post_meta( $id, ‘sfly_guest_author_email’, true );

    URL

    get_post_meta( $id, ‘sfly_guest_link’, true );

    Description/Bio

    get_post_meta( $id, ‘sfly_guest_author_description’, true )

    Thread Starter wprajshukla

    (@wprajshukla)

    Damn 😍
    Didn’t expected, will get reply soo fast.. Thanks a many Jones.
    Will let you know if i get stuck anywhere 🙂

    Plugin Author A. Jones

    (@nomadcoder)

    You are very welcome!

    Thread Starter wprajshukla

    (@wprajshukla)

    Hey Jones, I tried fetching the guest author’s meta but I guess even after it is fetching these three info – (Image, Bio and Name) its not how i want it to work, may be cuz guest author meta plugin is over riding the regular authors meta it is getting fetched but it does not work for those posts where guest author and regular author both are different. So unless until guest author’s are regular authors its not fetching data.
    Lets say A is guest author who is acting as a regular author for some blog post, his meta will get fetch, but if A is guest author to some post and B is regular author to same post, the meta of A is not getting fetched up. OR my code have some error, can you please give a check below 🙂 Thanks

    <?php
    						global $post;
    
    						$post_author_name = '';
    						$post_author_id = '';
    						$author_bio = '';
    
    						$guest_author_name = get_post_meta($post->ID, 'guest_author_name', true);
    
    						if (!empty($guest_author_name)) {
    							$post_author_name = $guest_author_name;
    							$author_bio = get_post_meta($post->ID, 'sfly_guest_author_description', true);
    						} else {
    							// Otherwise, use the regular author's information
    							$authors = get_coauthors($post->ID);
    
    							foreach ($authors as $author) {
    								$post_author_name = $author->display_name;
    								$post_author_id = $author->ID;
    								$author_bio = $author->description;
    								break;
    							}
    						}
    						?>
    
    						<div class="author-section">
    							<div class="author-content-box">
    								<div class="article-author-image">
    									<?php echo get_avatar($post_author_id, 150); ?>
    								</div>
    								<div class="author-hero-content">
    									<h4 class="author-name"><?php echo esc_html($post_author_name); ?></h4>
    									<?php if (!empty($author_bio)) : ?>
    										<p class="author-bio"><?php echo esc_html($author_bio); ?></p>
    									<?php endif; ?>
    								</div>
    							</div>
    						</div>
    
    Plugin Author A. Jones

    (@nomadcoder)

    Hello,

    The guest author meta is stored in the post meta. From first glance, your code looks ok.

    You need to use the guest author email address in order to retrieve the avatar. Something like this:

    $imageWidth = '150'; //The image size
    
    $imgUrl = 'http://www.gravatar.com/avatar/'.md5($userMail).'?size='.$imageWidth;
    
    Thread Starter wprajshukla

    (@wprajshukla)

    Hey Jones 🙂
    Hope u r doing good, once again i need your help understanding how can i fetch URL for guest author names. I have already tried using
    get_post_meta( $id, ‘sfly_guest_link’, true );
    but not working, here is the code I tried can you tell what could be the accurate way to fetch URL, I have created separate author pages for all guest and real authors. This code is currently fetching only real authors but not guest authors.

    Thanks 🙂

    <span class="author-name-title">
    
                                                    <?php
    
                                                    $author_name = get_the_author();
    
                                                    $is_guest_author = get_post_meta(get_the_ID(), 'sfly_guest_author_names', true);
    
                                                    if ($is_guest_author) {
    
                                                        // Retrieve guest author's name and URL from custom fields
    
                                                        $guest_author_name = get_post_meta(get_the_ID(), 'sfly_guest_author_names', true);
    
                                                        $guest_author_url = get_post_meta(get_the_ID(), 'sfly_guest_link', true);
    
                                                        // Output guest author's name linked to their respective author page
    
                                                        echo '<a href="' . esc_url($guest_author_url) . '">' . esc_html($guest_author_name) . '</a>';
    
                                                    } else {
    
                                                        // If not a guest author, link to the WordPress author page
    
                                                        $author_id = get_the_author_meta('ID');
    
                                                        $author_url = get_author_posts_url($author_id);
    
                                                        echo '<a href="' . esc_url($author_url) . '">' . esc_html($author_name) . '</a>';
    
                                                    }
    
                                                    ?>
    
    
    Plugin Author A. Jones

    (@nomadcoder)

    Hello! Could you send that code again? I don’t think that it came through properly!

    Thread Starter wprajshukla

    (@wprajshukla)

    Here is the entire <div>

                                            <div class="resource-footer">
    
                                                <span class="author-name-title">
    
                                                    <?php
    
                                                    $author_name = get_the_author();
    
                                                    $is_guest_author = get_post_meta(get_the_ID(), 'sfly_guest_author_names', true);
    
                                                    if ($is_guest_author) {
    
                                                        // Retrieve guest author's name and URL from custom fields
    
                                                        $guest_author_name = get_post_meta(get_the_ID(), 'sfly_guest_author_names', true);
    
                                                        $guest_author_url = get_post_meta(get_the_ID(), 'sfly_guest_link', true);
    
                                                        var_dump ($guest_author_url);
    
                                                        // Output guest author's name linked to their respective author page
    
                                                        echo '<a href="' . esc_url($guest_author_url) . '">' . esc_html($guest_author_name) . '</a>';
    
                                                    } else {
    
                                                        // If not a guest author, link to the WordPress author page
    
                                                        $author_id = get_the_author_meta('ID');
    
                                                        $author_url = get_author_posts_url($author_id);
    
                                                        echo '<a href="' . esc_url($author_url) . '">' . esc_html($author_name) . '</a>';
    
                                                    }
    
                                                    ?>
    
                                                </span>
    
                                                <span class="author-issue-data">
    
                                                    <?php echo get_the_date('M j, Y'); ?>
    
                                                </span>
    
                                            </div>
    
    
    Thread Starter wprajshukla

    (@wprajshukla)

    I guess the following function and key is responsbile to fetch the guest URL and even if they are working it is working for those fields which are coming under every blog post edit page on dashboard and not fetching from guest authors actual dashboard fields. 
    
    
    /**
    
             * guest_author_link function.
    
             * get the guest author url if one exists
    
             * @access public
    
             * @param string $link - real author link
    
             * @return string
    
             */
    
            function guest_author_link( $link ) {
    
                $id = $this->get_post_id();
    
                $author = esc_attr( get_post_meta( $id, 'sfly_guest_author_names', true ));
    
                if ( $author )
    
                {
    
                    $link = get_post_meta( $id, 'sfly_guest_link', true );
    
                    if (!$link)
    
                        $link = "";
    
                }
    
                return $link;
    
            }
    Plugin Author A. Jones

    (@nomadcoder)

    This looks correct to me.

    Thread Starter wprajshukla

    (@wprajshukla)

    I am unable to call the URL which is saved in Authors profile.
    Where there is a button next to the field that is “View”, this buttons takes to the author page itself …. mysite.com/author/guest-author-name

    URL key is not working I guess for the author profile
    HOW can I call the URL data from there ?

    Plugin Author A. Jones

    (@nomadcoder)

    I am really not clear on the issue. The author url should work on a post, out of the box. Try another theme to see if it’s working on that theme. If not, let me know more about your theme.

    To clarify, you are writing custom code and not able to retrieve the URL for the guest author? If this is the case, your code looks correct. I do not have enough information to figure out what is going wrong.

    • This reply was modified 1 year, 2 months ago by A. Jones.
Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Fetching Guest Author Meta’ is closed to new replies.