- This topic has 9 replies, 2 voices, and was last updated 3 years, 8 months ago by Tom.
-
AuthorPosts
-
August 7, 2020 at 3:12 pm #1392174Joey
I use this code to add guest authors:
add_filter( 'the_author', 'guest_author_name' ); add_filter( 'get_the_author_display_name', 'guest_author_name' ); function guest_author_name( $name ) { global $post; $author = get_post_meta( $post->ID, 'guest-author', true ); if ( $author ) $name = $author; return $name; }
Then in custom fields I add a “guest-author” field. This way I don’t have to create a contributor account for writers who only posts one time.
The problem is that their name at the top of the page links to the real author (the generic “guest author” account) page and puts the wrong name at the top (the most recent post). I don’t mind having a generic author page for all the guest writers but it can’t have the wrong name. For example: https://simipress.com/author/simieditions/
Is there a way to modify the above snippet so that these guest authors don’t have the default link to the author page on their name?
August 8, 2020 at 9:20 am #1393100TomLead DeveloperLead DeveloperHi there,
You may be able to use this filter:
add_filter( 'author_link', function( $link ) { $author = get_post_meta( get_the_ID(), 'guest-author', true ); if ( $author ) { $link = 'custom link'; } return $link; } );
August 8, 2020 at 5:57 pm #1393401JoeyThank you Tom, for the quick reply. I got the error:
Your PHP code changes were rolled back due to an error on line 107 of file wp-content/themes/generatepress_child/functions.php. Please fix and try saving again.
syntax error, unexpected ‘;’
Line 107 is this:
$author = get_post_meta( get_the_ID(), ‘guest-author’, true );August 9, 2020 at 10:25 am #1394266TomLead DeveloperLead DeveloperSorry about that – can you try the updated code?
August 9, 2020 at 5:39 pm #1394584JoeyWeird, it works well on Firefox, but for Chrome or mobile it takes me to custom20%link as a separate page saying “This site can’t be reached.” Any idea why?
If this is too much trouble, another solution might be to just link guest authors to a static page (my contributor page).
August 10, 2020 at 9:30 am #1395764TomLead DeveloperLead DeveloperDid you update
custom link
with the full URL where the link should go?August 10, 2020 at 7:22 pm #1396365JoeyOh, sorry I’m being stupid. That works. If I want to remove the hyperlink completely (nothing happens when they click on the author name) for guest authors, how would I modify the code? I know I can use a hashtag or just leave it blank, but then it reloads the page. Thank you.
August 11, 2020 at 9:08 am #1397422TomLead DeveloperLead DeveloperIn that case, you could try this:
add_filter( 'generate_post_author_output', function( $output ) { $author = get_post_meta( get_the_ID(), 'guest-author', true ); if ( $author ) { return sprintf( '<span class="byline">%1$s<span class="author vcard" %3$s><span class="author-name" itemprop="name">%2$s</span</span></span> ', apply_filters( 'generate_inside_post_meta_item_output', '', 'author' ), esc_html( get_the_author() ), generate_get_microdata( 'post-author' ) ); } return $output; } );
August 11, 2020 at 1:09 pm #1397708JoeyVery cool, it works. Thank you very much. I need to learn PHP.
August 11, 2020 at 4:31 pm #1397946TomLead DeveloperLead DeveloperGlad I could help! 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.