Site logo

Archived topic

Remove link for the author archive page on author name (specific posts)

9 replies · Started by Joey on August 7, 2020

Viewing posts 1–10 of 10

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?

Hi 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;
} );

Thank 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 );

Sorry about that - can you try the updated code?

Weird, 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).

Did you update custom link with the full URL where the link should go?

Oh, 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.

In 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;
} );

Very cool, it works. Thank you very much. I need to learn PHP.

Glad I could help! :)

This archived topic is closed to new replies.